Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > String and Char Help

Reply
Thread Tools

String and Char Help

 
 
BlackJackal
Guest
Posts: n/a
 
      01-31-2007
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

 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      01-31-2007
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/
 
Reply With Quote
 
 
 
 
Alex Hunsley
Guest
Posts: n/a
 
      01-31-2007
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....
 
Reply With Quote
 
cp
Guest
Posts: n/a
 
      01-31-2007
Your counting is off. And is you bothered to check your for-loop with the
syntax you'd probably locate the error.


 
Reply With Quote
 
cp
Guest
Posts: n/a
 
      01-31-2007
Oh and you should really consider replacing the if x || y || z.... with a
switch. It clutters the code otherwise.


 
Reply With Quote
 
Michael Rauscher
Guest
Posts: n/a
 
      02-01-2007
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
 
Reply With Quote
 
Michael Rauscher
Guest
Posts: n/a
 
      02-01-2007
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
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-01-2007
Michael Rauscher wrote:
> Which would be wrong, use
>
> String vowels = "aAeEIiOoUu";
>
> instead


"À*ÁáÂâÃãÄäÅ寿ÈèÉéÊêËëÌìÍ*Î îÏïÒòÓóÔôÕõÖöÙùÚúÛûÜüÝýŁł ŒœαεΗηΥυΩωЮюийאוי" ...

- Lew
 
Reply With Quote
 
Michael Rauscher
Guest
Posts: n/a
 
      02-02-2007
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
 
Reply With Quote
 
Randolf Richardson
Guest
Posts: n/a
 
      02-02-2007
On Wed, 31 Jan 2007 21:34:26 -0800, Michael Rauscher <>
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+
The Lumber Cartel, local 42 (Canadian branch)
http://www.lumbercartel.ca/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
Problem- strcat with char and char indexed from char array aldonnelley@gmail.com C++ 3 04-20-2006 07:32 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
The difference between char a[6] and char *p=new char[6] ? wwj C Programming 24 11-07-2003 05:27 PM
the difference between char a[6] and char *p=new char[6] . wwj C++ 7 11-05-2003 12:59 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57