I have this task where I'm supposed to write a method that counts number of consonants in the input word. I'm not sure what the problem is since my output is always 0 (same as declared in the beginning with int number=0;
) and when I try without declaring the value It just gives me error on method calling. Are there any mistakes that I'm not aware of?
The code:
import java.util.*;
public class Student {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
String beseda;
int number=0;;
System.out.println("Please input your string: " );
beseda = scan.nextLine();
new Work().prestej(beseda, number);
System.out.println("The number of consonants is: " + number);
}
}
class Work {
int prestej(String beseda, int number) {
int lenght = beseda.length();
for (int i=0; i<lenght; i++) {
if (beseda.charAt(i) != 'a' || beseda.charAt(i) != 'u' || beseda.charAt(i) != 'e' || beseda.charAt(i) != 'i' || beseda.charAt(i) != 'o' ) {
number++;
} else {
break;
}
}
return number;
}
}