Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Counting Char's Within Strings

Reply
Thread Tools

Counting Char's Within Strings

 
 
BlackJackal
Guest
Posts: n/a
 
      01-31-2007
Alright a couple of stupid questions here about strings and Chars.
First off 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");
}
}

First question is why does String1.length() return 70 when I only
count 69? The other question is why does this code always produce 0
vowels?

Thanks in advance I am just a little stumped.

 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      01-31-2007
BlackJackal wrote:
> Alright a couple of stupid questions here about strings and Chars.
> First off 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");
> }
> }
>
> First question is why does String1.length() return 70 when I only
> count 69?


Because you can't count.

> The other question is why does this code always produce 0
> vowels?


The problem is in this line!

> for(i = 0; i < length - 1 ; i++);


--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
 
 
 
Alex Hunsley
Guest
Posts: n/a
 
      01-31-2007
BlackJackal wrote:
> Alright a couple of stupid questions here about strings and Chars.
> First off 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");
> }
> }
>
> First question is why does String1.length() return 70 when I only
> count 69? The other question is why does this code always produce 0
> vowels?
>
> Thanks in advance I am just a little stumped.


Please in future post code that is directly compilable. Your long
strings (>80 chars or so) are broken in your post, and have to be
manually fixed in order to compile. In future, this style helps:

String longString = "This is a really "
+ "long string I am making so "
+ "I will split it up like this";

As for your no vowels counted problem - it's a subtle one, this: you've
put a ';' after your for statement. This means what you think is the
body of your for is actually a separate statement that run after the for
loop. The for loop itself is running from 0 to 69 and doing nothing (
each time. Remove the ';' at end of the for line and it works.

You only count 69 chars? You sure? Count again... did you include the
full stop at the end? Did you actually count them, or are you being
mislead by the fact the for loop goes up to 'length - 1'?

Finally, that horrid big 'if' check for the vowels can be better written
this way:

if ("AaEeIiOoUu".indexOf(pos) >= 0) {
vowel += 1;
}

If you make sure pos contains a lower-case char, you could even just do
"aeiou".indexOf...

lex


 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      02-01-2007
On Jan 31, 1:51 pm, Knute Johnson <(E-Mail Removed)>
wrote:
> BlackJackal wrote:
> > Alright a couple of stupid questions here about strings and Chars.
> > First off 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");
> > }
> > }

>
> > First question is why does String1.length() return 70 when I only
> > count 69?

>
> Because you can't count.
>
> > The other question is why does this code always produce 0
> > vowels?

>
> The problem is in this line!
>
> > for(i = 0; i < length - 1 ; i++);

>
> --
>
> Knute Johnson
> email s/nospam/knute/



You should be nice and tell him why that line is wrong.
Since you have a ; after the ), it tells that for loop to do *nothing*
for all i between [0, length-1)
it really should be
for (i = 0; i < length; ++i) {
// do vowal county bit.
}

 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      02-01-2007
Daniel Pitts wrote:
> On Jan 31, 1:51 pm, Knute Johnson <(E-Mail Removed)>
> wrote:
>> BlackJackal wrote:
>>> Alright a couple of stupid questions here about strings and Chars.
>>> First off 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");
>>> }
>>> }
>>> First question is why does String1.length() return 70 when I only
>>> count 69?

>> Because you can't count.
>>
>>> The other question is why does this code always produce 0
>>> vowels?

>> The problem is in this line!
>>
>> > for(i = 0; i < length - 1 ; i++);

>>
>> --
>>
>> Knute Johnson
>> email s/nospam/knute/

>
>
> You should be nice and tell him why that line is wrong.
> Since you have a ; after the ), it tells that for loop to do *nothing*
> for all i between [0, length-1)
> it really should be
> for (i = 0; i < length; ++i) {
> // do vowal county bit.
> }
>


I guess it is difficult to express 'ribbing' through the post.

As to the ;, I was trying to be helpful but not give the fellow the
direct answer as we have all done this at one time or another. Finding
being its own great reward, I thought he would see what I posted and
slap his head and say "Oh that was dumb", as I have on several
occasions. In fact it took me a couple minutes to find it this time.

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
Luc The Perverse
Guest
Posts: n/a
 
      02-01-2007
"Knute Johnson" <(E-Mail Removed)> wrote in message
news:4bcwh.154443$(E-Mail Removed)...
> I guess it is difficult to express 'ribbing' through the post.
>
> As to the ;, I was trying to be helpful but not give the fellow the direct
> answer as we have all done this at one time or another. Finding being its
> own great reward, I thought he would see what I posted and slap his head
> and say "Oh that was dumb", as I have on several occasions. In fact it
> took me a couple minutes to find it this time.


I have seen classes where ";" is simply described as the end of a
statement - which I suppose is not specifically "wrong" but to someone who
had misinterpreted it early on could cause continued frustration.

It would be good for any instructor to head this one off by not only showing
how the "mistake" could be made, but by showing how in certain circumstances
doing nothing inside a loop would make sense.

--
LTP




 
Reply With Quote
 
Proton Projects - Moin
Guest
Posts: n/a
 
      02-01-2007
Hi Knute,

How this code will help to find vowels in a particular string

if ("AaEeIiOoUu".indexOf(pos) >= 0) {
vowel += 1;
}


"AaEeIiOoUu" is a complete string....

Or else u r asking to do like this

"A".indexOf(pos)>=0
"E".indexOf(pos)>=0
"I".indexOf(pos)>=0
"O".indexOf(pos)>=0
"U".indexOf(pos)>=0

Thanks,
Moin

On Feb 1, 7:24 am, Knute Johnson <(E-Mail Removed)>
wrote:
> Daniel Pitts wrote:
> > On Jan 31, 1:51 pm, Knute Johnson <(E-Mail Removed)>
> > wrote:
> >> BlackJackal wrote:
> >>> Alright a couple of stupid questions here about strings and Chars.
> >>> First off 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");
> >>> }
> >>> }
> >>> First question is why does String1.length() return 70 when I only
> >>> count 69?
> >> Because you can't count.

>
> >>> The other question is why does this code always produce 0
> >>> vowels?
> >> The problem is in this line!

>
> >> > for(i = 0; i < length - 1 ; i++);

>
> >> --

>
> >> Knute Johnson
> >> email s/nospam/knute/

>
> > You should be nice and tell him why that line is wrong.
> > Since you have a ; after the ), it tells that for loop to do *nothing*
> > for all i between [0, length-1)
> > it really should be
> > for (i = 0; i < length; ++i) {
> > // do vowal county bit.
> > }

>
> I guess it is difficult to express 'ribbing' through the post.
>
> As to the ;, I was trying to be helpful but not give the fellow the
> direct answer as we have all done this at one time or another. Finding
> being its own great reward, I thought he would see what I posted and
> slap his head and say "Oh that was dumb", as I have on several
> occasions. In fact it took me a couple minutes to find it this time.
>
> --
>
> Knute Johnson
> email s/nospam/knute/



 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      02-01-2007
Proton Projects - Moin wrote:

> Hi Knute,
>
> How this code will help to find vowels in a particular string
>
> if ("AaEeIiOoUu".indexOf(pos) >= 0) {
> vowel += 1;
> }
>
>
> "AaEeIiOoUu" is a complete string....
>
> Or else u r asking to do like this
>
> "A".indexOf(pos)>=0
> "E".indexOf(pos)>=0
> "I".indexOf(pos)>=0
> "O".indexOf(pos)>=0
> "U".indexOf(pos)>=0


No, he meant exactly what he wrote: the expression

"AaEeIiOoUu".indexOf(pos) >= 0

is true iff the character `pos` is one of AaEeIiOoUu.

PS Is `y` a vowel?

--
Chris "electrick hedgehog" Dollin
"We live for the One, you die for the One." Unsaid /Babylon 5/.

 
Reply With Quote
 
Jeff Higgins
Guest
Posts: n/a
 
      02-01-2007

Chris Dollin wrote:
>
> PS Is `y` a vowel?
>

Wow! Thanks for asking. That was great fun.
[http://en.wikipedia.org/wiki/Vowel#Written_vowels]

--
Jeff Higgins


 
Reply With Quote
 
voorth
Guest
Posts: n/a
 
      02-01-2007
On Jan 31, 10:27 pm, "BlackJackal" <(E-Mail Removed)> wrote:
> Alright a couple of stupid questions here about strings and Chars.


The simplest way, of course, uses regular expressions:

int countVowels(String input) {
return input.replaceAll("[^aeiouyAEIOUY]", "").size();
}

 
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
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
Counting number of strings literals in double dimentional char array ranjmis C Programming 14 03-12-2006 11:51 PM
counting up instead of counting down edwardfredriks Javascript 6 09-07-2005 03:30 PM
Counting occurences of words in a list of strings Travers Naran Python 4 05-25-2005 05:58 AM
Comparing strings from within strings Rick C Programming 3 10-21-2003 09:10 AM



Advertisments