String and Char Help  
Author Message
BlackJackal





PostPosted: 2007-2-1 5:44:00 Top

java-programmer, String and Char Help I have a few questions but first here is my code.

public class CountVowels
{
public static void main(String[] args)
{
int vowel = 0;
int i;
char pos;
String String1 = "Event Handlers is dedicated to making your
event a most memorable one.";
int length = String1.length();
for(i = 0; i < length - 1 ; i++);
{
pos = String1.charAt(i);
if (pos == 'A' || pos == 'a' || pos == 'E' || pos == 'e' ||
pos == 'I' || pos == 'i' || pos == 'O' || pos == 'o' || pos == 'U' ||
pos == 'u') {
vowel += 1;
}
}
System.out.println("There are " + vowel + " vowels in this
String");
}
}

Question one is why does String1.length() return 70 when there are
only 69 chars. Second question is why does this code not count the
vowels in the String.

Thanks in advance

Robert

 
Knute Johnson





PostPosted: 2007-2-1 5:53:00 Top

java-programmer >> String and Char Help BlackJackal wrote:
> I have a few questions but first here is my code.
>
> public class CountVowels
> {
> public static void main(String[] args)
> {
> int vowel = 0;
> int i;
> char pos;
> String String1 = "Event Handlers is dedicated to making your
> event a most memorable one.";
> int length = String1.length();
> for(i = 0; i < length - 1 ; i++);
> {
> pos = String1.charAt(i);
> if (pos == 'A' || pos == 'a' || pos == 'E' || pos == 'e' ||
> pos == 'I' || pos == 'i' || pos == 'O' || pos == 'o' || pos == 'U' ||
> pos == 'u') {
> vowel += 1;
> }
> }
> System.out.println("There are " + vowel + " vowels in this
> String");
> }
> }
>
> Question one is why does String1.length() return 70 when there are
> only 69 chars. Second question is why does this code not count the
> vowels in the String.
>
> Thanks in advance
>
> Robert
>

You still can't count and the problem is in the for loop!

--

Knute Johnson
email s/nospam/knute/
 
Alex Hunsley





PostPosted: 2007-2-1 6:07:00 Top

java-programmer >> String and Char Help BlackJackal wrote:
> I have a few questions but first here is my code.
>
> public class CountVowels
> {
> public static void main(String[] args)
> {
>
[snip]

Didn't you just post this very same question 20 mins ago?
Hint: newsgroup posts don't always appear immediately. Give it at least
a couple of hours before assuming your message may have gone astray.
Annoying, I know....
 
 
cp





PostPosted: 2007-2-1 6:26:00 Top

java-programmer >> String and Char Help Your counting is off. And is you bothered to check your for-loop with the
syntax you'd probably locate the error.


 
 
cp





PostPosted: 2007-2-1 6:30:00 Top

java-programmer >> String and Char Help Oh and you should really consider replacing the if x || y || z.... with a
switch. It clutters the code otherwise.


 
 
Michael Rauscher





PostPosted: 2007-2-1 13:34:00 Top

java-programmer >> String and Char Help cp schrieb:
> Oh and you should really consider replacing the if x || y || z.... with a
> switch. It clutters the code otherwise.
>
>
In fact, I'd replace the whole if/switch thing with a simple

String vowels = "aAeEoOuU";

if ( vowels.indexOf(pos) != -1 )

or with a Map or ...

Bye
Michael
 
 
Michael Rauscher





PostPosted: 2007-2-2 6:36:00 Top

java-programmer >> String and Char Help Michael Rauscher schrieb:
> cp schrieb:
>> Oh and you should really consider replacing the if x || y || z....
>> with a switch. It clutters the code otherwise.
>>
> In fact, I'd replace the whole if/switch thing with a simple
>
> String vowels = "aAeEoOuU";

Which would be wrong, use

String vowels = "aAeEIiOoUu";

instead :)

Bye
Michael
 
 
Lew





PostPosted: 2007-2-2 7:28:00 Top

java-programmer >> String and Char Help Michael Rauscher wrote:
> Which would be wrong, use
>
> String vowels = "aAeEIiOoUu";
>
> instead :)

"脌脿脕谩脗芒脙茫脛盲脜氓脝忙脠猫脡茅脢锚脣毛脤矛脥铆脦卯脧茂脪貌脫贸脭么脮玫脰枚脵霉脷煤脹没脺眉脻媒艁艂艗艙伪蔚螚畏违蠀惟蠅挟褞懈泄讗讜讬" ...

- Lew
 
 
Michael Rauscher





PostPosted: 2007-2-2 10:22:00 Top

java-programmer >> String and Char Help Lew schrieb:
> Michael Rauscher wrote:
>> Which would be wrong, use
>>
>> String vowels = "aAeEIiOoUu";
>>
>> instead :)
>
> "脌脿脕谩脗芒脙茫脛盲脜氓脝忙脠猫脡茅脢锚脣毛脤矛脥铆脦卯脧茂脪貌脫贸脭么脮玫脰枚脵霉脷煤脹没脺眉脻媒艁艂艗艙伪蔚螚畏违蠀惟蠅挟褞懈泄讗讜讬" ...

OP:

if (pos == 'A' || pos == 'a' || pos == 'E' || pos == 'e' ||
pos == 'I' || pos == 'i' || pos == 'O' || pos == 'o' || pos == 'U' ||
pos == 'u')

==>

"脌脿脕谩脗芒脙茫脛盲脜氓脝忙脠猫脡茅脢锚脣毛脤矛脥铆脦卯脧茂脪貌脫贸脭么脮玫脰枚脵霉脷煤脹没脺眉脻媒艁艂艗艙伪蔚螚畏违蠀惟蠅挟褞懈泄讗讜讬" ?!?

;)

Bye
Michael
 
 
Randolf Richardson





PostPosted: 2007-2-2 11:27:00 Top

java-programmer >> String and Char Help On Wed, 31 Jan 2007 21:34:26 -0800, Michael Rauscher <email***@***.com>
wrote:
> cp schrieb:
>
>> Oh and you should really consider replacing the if x || y || z.... with
>> a switch. It clutters the code otherwise.
>In fact, I'd replace the whole if/switch thing with a simple
>
> String vowels = "aAeEoOuU";

And sometimes i? ;-)

I'd also declare it like this:

final String VOWELS = "aeiouAEIOU";

> if ( vowels.indexOf(pos) != -1 )
[sNip]

The reason I'd re-order the variables to begin with lower-case is for
efficiency since words are almost always mostly made up of combinations of
lower-case letters, but of course the application of this must also be
considered. Anyway, by placing the more commonly used variations first,
fewer iterations by the String.indexOf() method are required, and the
application works just a tiny bit faster as a result.

Now, with that in mind, if this little search is being used repeatedly in
a loop, then the benefits of such optimizations become more clear. With
regards to optimization, one may wish to also investigate the possibility
that "e" may be more common than "a" or that some other order would be
better suited.

--
Randolf Richardson - kingpin+email***@***.com
The Lumber Cartel, local 42 (Canadian branch)
http://www.lumbercartel.ca/
 
 
Chris Uppal





PostPosted: 2007-2-3 0:46:00 Top

java-programmer >> String and Char Help Randolf Richardson wrote:

> I'd also declare it like this:
>
> final String VOWELS = "aeiouAEIOU";
>
> > if ( vowels.indexOf(pos) != -1 )
> [sNip]
>
> The reason I'd re-order the variables to begin with lower-case is for
> efficiency since words are almost always mostly made up of combinations of
> lower-case letters, but of course the application of this must also be
> considered. Anyway, by placing the more commonly used variations first,
> fewer iterations by the String.indexOf() method are required, and the
> application works just a tiny bit faster as a result.

It would be interesting to know whether the overhead of the method call (and
it's internal logic too) would be quicker than an explicit switch statement.

Actually, it's quite hard to imagine a situation where the performance of
either technique would matter much -- bulk text is by its very nature IO
limited, and if there isn't a huge bulk of text to scan, why should ultra-fast
scanning be worth the effort ? (Except for pure intellectual interest, of
course).

-- chris