Re: Counting Char's Within Strings



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


.



Relevant Pages

  • RE: Removing words from a string?
    ... a1 = "1 high street leeds yorkshire" ... My problem comes in working out how long the string is (yes I know LEN, ... I want to be able to say, ok pos 2 is a space, where is the next one? ... though I could concatenate an XX char so I know where it ends. ...
    (microsoft.public.excel.programming)
  • RE: Removing words from a string?
    ... a1 = "1 high street leeds yorkshire" ... My problem comes in working out how long the string is (yes I know LEN, ... I want to be able to say, ok pos 2 is a space, where is the next one? ... though I could concatenate an XX char so I know where it ends. ...
    (microsoft.public.excel.programming)
  • RE: Removing words from a string?
    ... Columns will populate the 'Convert Text to Columns Wizard'. ... My problem comes in working out how long the string is (yes I know LEN, ... I want to be able to say, ok pos 2 is a space, where is the next one? ... though I could concatenate an XX char so I know where it ends. ...
    (microsoft.public.excel.programming)
  • Re: String and Char Help
    ... int vowel = 0; ... pos = String1.charAt; ... vowels in the String. ...
    (comp.lang.java.programmer)
  • String interpolation
    ... I'm trying to do some string interpolation -- that is, ... static char* expand(const char *format) ... strncat(string, format, pos); ...
    (comp.lang.c)