First of all, excuse me for my bad english. It's not my native language (this is one of my problems, you'll see later why).
I'm making a method in Java and it is proposed to count vowels of a String. The input comes from windows prompt, because i'm compiling and executing using javac and java commands, respectively.
I've written some lines of code to solve the exercise but i can't count vowels which have accent marks. eg. When i try "canción", its output just count 2 vowels.
public static int countVowels(String text){
int answer = 0;
String test = "AEIOUaeiou";
for (int i = 0; i < text.length(); i++)
for (int j = 0; j < test.length(); j++)
if (text.charAt(i) == test.charAt(j)) {
answer++;
break;
}
return answer;
}
public static void form() {
Scanner in = new Scanner(System.in);
System.out.println("This program counts the number of vowels.");
int answer = 0;
String text = "";
while (!text.equals("-1")) {
System.out.print("Enter a line of text (-1 to exit): ");
text = in.nextLine();
answer = Character.countVowels(text);
System.out.println("Number of vowels: " + answer);
}
System.out.println("Closing the program...");
}
I'm using Scanner as input method.
I tried comparing 2 Strings, 'cause i think using if(text.charAt(i) == 'A')
is just boring (Maybe i'm wrong).
Really I don´t know what to do. I've tried using Ascii values (193 = Á), I tried just writing de vowels with accent mark in the test String String test = "AEIOUaeiouÁÉÍÓÚáéíóú";
.
Nothing works. I'll be very grateful to the person who can help me.
PS, i'm not allowed to use array.