counting vowels
I've been beating my brains trying to figure out why this program won't output the number vowels. Can someone point me in the right direction?
import java.util.Scanner;
publicclass CountVowels
{
static Scanner console =new Scanner(System.in);
static String str;
staticint numberOfCharacters;
staticint vowelCount = 0;
staticint i;
staticchar letter;
publicstaticvoid main (String[] args)
{
System.out.print("Enter a string of characters: ");
str = console.next();
numberOfCharacters = str.length();
for (int i = 0; i < numberOfCharacters; i++)
{
if (isVowel(str.charAt(i)) ==true)
vowelCount++;
}
System.out.println();
System.out.println("Number of vowels = " + letter);
System.out.println();
}
publicstaticboolean isVowel(char letter)
{
letter = str.charAt(i);
if (letter =='a' || letter =='A' ||
letter =='e' || letter =='E' ||
letter =='i' || letter =='I' ||
letter =='o' || letter =='O' ||
letter =='u' || letter =='U'
)
returntrue;
returnfalse;
}
}
[2736 byte] By [
mattvgta] at [2007-11-27 10:00:20]

public static boolean isVowel(char letter)
{
//letter = str.charAt(i); DELETE THIS LINE
if (letter == 'a' || letter == 'A' ||
letter == 'e' || letter == 'E' ||
letter == 'i' || letter == 'I' ||
letter == 'o' || letter == 'O' ||
letter == 'u' || letter == 'U'
)
return true;
return false;
}
Also in terms of style
this
if (isVowel(str.charAt(i)) == true)
vowelCount++;
would be better as
if (isVowel(str.charAt(i)))
{
vowelCount++;
}
Same result with:
import java.util.Scanner;
public class CountVowels
{
static Scanner console = new Scanner(System.in);
static String str;
static int numberOfCharacters;
static int vowelCount = 0;
static int i;
static char letter;
public static void main (String[] args)
{
System.out.print("Enter a string of characters: ");
str = console.next();
numberOfCharacters = str.length();
for (int i = 0; i < numberOfCharacters; i++)
{
if (isVowel(str.charAt(i)) == true)
vowelCount++;
}
System.out.println();
System.out.println("Number of vowels = " + letter);
System.out.println();
}
public static boolean isVowel(char letter)
{
if (letter == 'a' || letter == 'A' ||
letter == 'e' || letter == 'E' ||
letter == 'i' || letter == 'I' ||
letter == 'o' || letter == 'O' ||
letter == 'u' || letter == 'U'
)
return true;
return false;
}
}
What is this supposed to do?System.out.println("Number of vowels = " + letter);
Ok I've changed it toif (isVowel(str.charAt(i))){ vowelCount++;}
System.out.println("Number of vowels = " + letter);Output the number of vowels in the string.
> Same result with:You have multiple problems but fixing that one was a start. See my last post.
> > System.out.println("Number of vowels = " + letter);> > > Output the number of vowels in the string.Really now?You want to explain where you output the number of vowels in the string?
Should I make another variable for the number of vowels?
> Should I make another variable for the number of> vowels?You already have another variable for the number of vowels.
I changed System.out.println("Number of vowels = " + letter);toSystem.out.println("Number of vowels = " + vowelCount);It seems to work correctly now.
One more question...If I deleted:letter = str.charAt(i)Why did is it needed here:if (isVowel(str.charAt(i)) == true)Message was edited by: mattvgt
> One more question...
>
> If I deleted:
>
> > letter = str.charAt(i)
>
>
> Why did is it needed here:
>
> > if (isVowel(str.charAt(i)) == true)
>
>
In your main method you call isVowel passing it the right character as a char from the String.
When you add the letter = str.charAt(i) line in the isVowel method you were wiping out the letter and replacing it with the very first letter of the String each time.
For example if the String was "The" the code now says 1 vowel. But before it would have said zero. Why? Because the comparison is now 'T', 'h' and 'e' but before it would have been 'T','T' and 'T'.
Part of your confusion is stemming from too many static variables and class and local variables that conflict. Again for example the i static variable. It does nothing and is not the same i as the one you create in your for loop.
Is this a better way of doing it?
import java.util.Scanner;
public class CountVowels
{
static Scanner console = new Scanner(System.in);
static int i;
public static void main (String[] args)
{
String str;
int numberOfCharacters;
int vowelCount = 0;
System.out.print("Enter a string of characters: ");
str = console.next();
numberOfCharacters = str.length();
for (int i = 0; i < numberOfCharacters; i++)
if (isVowel(str.charAt(i)) == true)
{
vowelCount++;
}
System.out.println();
System.out.println("Number of vowels = " + vowelCount);
System.out.println();
}
public static boolean isVowel(char letter)
{
if (letter == 'a' || letter == 'A' ||
letter == 'e' || letter == 'E' ||
letter == 'i' || letter == 'I' ||
letter == 'o' || letter == 'O' ||
letter == 'u' || letter == 'U')
return true;
return false;
}
}
As already pointed out the static variable i is pointless. It is different to the variable i you declare in the for loop and never gets used. Delete it.
As also pointed out doing comparisons on booleans is pointless. That is, never do this:
if(something == true)
//when this is better
if(something)
Lastly, your if statement in the isVowel method is confusing. It looks like it has two return statements next to each other. Try better indentation and put braces { } around it. Even if an if statement has only one line and you don't have to use braces, get into the habit of using them anyway.