public class WordPlay { public boolean isVowel(char ch) { String vowels = "aeiouAEIOU"; int i; for (i = 0; i < vowels.length(); i = i + 1) { char v = vowels.charAt(i); if (v == ch) { return true; } else { return false; } } } public void testIsVowel() { char value = isVowel('a'); System.out.println(value); } }
I am getting an error saying: “cannot convert boolean to char” for line 15 (that is the line: char value = isVowel('a')
).
My last method is a tester method to see if the isVowel
char will print true or false for any char that I place in there. Very new to Java don’t know how to continue from here.
Advertisement
Answer
So there are a few things wrong with your loop. So I decided to give you a refactored code.
public boolean isVowel(char ch) { String vowels = "aeiouAEIOU"; boolean isVowel = false; //the int can be declared in the loop. There is no need for i = i + 1 for (int i = 0; i < vowels.length(); i++) { if (vowels.charAt(i) == ch) { //If there is a vowel, just exit the loop with the result. isVowel = true; break; } } return isVowel; }