First time posting here, however have looked here for resources many times.
I'm trying to create an application that will count the number of vowels and consonants that are typed in into the computer. None of my code is underlined, However my application bugs out partway through. I ran Debugger and everything seems fine untill the point that i bolded. It throws an out of bounds error. Here is my code:
package vowelCons;
import java.util.Scanner;
public class VowelCons {
public static void main(String args[]){
String input;
char selection;
int x = 0;
char[] a = new char[x];
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a string: ");
input = keyboard.nextLine();
x = input.length();
//Convert from String to Char
for (int p = 0; p < x; p++) { // This is where it errors out
a[p] = input.charAt(p); // in these couple lines here.
// It gives me an out of bounds error.
}
// Determine amount of Vowels and Cons
int Vowels = 0;
int Cons = 0;
for (int i = 0; i < 100 ; i++){
if (a[i] == 'a' || a[i] == 'e' || a[i] == 'i' || a[i] == 'o' || a[i] == 'u'){
Vowels++;
}else{
Cons++;
}
}
// Switch case to determine which Menu was Selected and Print corresponding Results
do{
selection = getMenuSelection();
switch(Character.toLowerCase(selection))
{
case 'a' : System.out.println("\nNumber of vowels: " + Vowels);
break;
case 'b' : System.out.println("\nNumber of Consonats: " + Cons);
break;
case 'c' : System.out.println("\nNumber of vowels: " + Vowels);
System.out.println("\nNumber of Consonants: " + Cons);
break;
case 'd': System.out.print("Enter a String: ");
input = keyboard.nextLine();
break;
}
} while (Character.toLowerCase(selection)!='e');
}
public static char getMenuSelection() {
String input;
char selection;
Scanner keyboard = new Scanner(System.in);
System.out.println("a) Count the number of vowels in the string.");
System.out.println("b) Count the number of Consonants in the string.");
System.out.println("c) Count the number of vowels and consonants in the string.");
System.out.println("d) Enter anohter string.");
System.out.println("e) Exit the program.");
input = keyboard.nextLine();
selection = input.charAt(0);
while(Character.toLowerCase(selection)<'a' || Character.toLowerCase(selection) > 'e')
{
System.out.print("Only enter a, b, c, d, or e: ");
input = keyboard.nextLine();
selection = input.charAt(0);
}
return selection;
}
}