import java.util.Scanner;
public class FAvowelsCount {
public static void main(String[] args) {
String checkForVowels;
int[] array = new int[6]; //a,e,i,o,u,non-vowels
String[] arrayString = {"A","E","I","O","U","Non-Vowels"};
//Scan the string:
Scanner input = new Scanner(System.in);
System.out.println("Enter a string");
checkForVowels = input.nextLine();
//read each character in the string by looping
for(int x = 0; x < checkForVowels.length(); x++){
//if the character being read
switch (checkForVowels.charAt(x)) {
case 'a': array[0]++; break;
case 'e': array[1]++; break;
case 'i': array[2]++; break;
case 'o': array[3]++; break;
case 'u': array[4]++; break;
default: array[5]++; break;
}
}
//results
int x = 0;
for(int y : array){
System.out.println(arrayString[x++]+ " " + y);
}
}
}
This is a program that counts the occurrences of vowels and non-vowels and then prints them out.
The program counts the vowels alright, for example, if I enter a string: a e i o u f
It would print something like this:
a: 1
e: 1
I: 1
o: 1
u: 1
non-vowels: 6 <<<<<-- This is not correct. The default in the switch statement for some reason I think isn't working right.


Sign In
Create Account


Back to top









