Re: String and Char Help
- From: Knute Johnson <nospam@xxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 31 Jan 2007 13:53:18 -0800
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/
.
- References:
- String and Char Help
- From: BlackJackal
- String and Char Help
- Prev by Date: Re: Counting Char's Within Strings
- Next by Date: Simple java test
- Previous by thread: String and Char Help
- Next by thread: Re: String and Char Help
- Index(es):
Relevant Pages
|