Results 1 to 4 of 4
Thread: Counting Vowels and Constonants
- 11-19-2007, 09:34 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 2
- Rep Power
- 0
Counting Vowels and Constonants
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:
Java 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++) { [B] // This is where it errors out[/B] a[p] = input.charAt(p); [B] // in these couple lines here. [/B] [B]// It gives me an out of bounds error.[/B] } // 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; } }
- 11-19-2007, 09:51 PM #2
You problem seems to be with getting the length of your line. Try printing x before you enter the for loop and print p inside of the for loop so you will know have many times it loops.
Is it possible that the nextLine() method of the Scanner class doesn't work the way that you think it does? I don't deal with Scanners very much so I'm afraid that I'm not to familiar with them and their methods. I could be very wrong.
- 11-20-2007, 10:52 AM #3
Member
- Join Date
- Aug 2007
- Posts
- 26
- Rep Power
- 0
From the code you have posted, I see a problem in this line:
char[] a = new char[x];
you have initialized int x=0;, so there is only one element in the character array and thats the reason for out of bound exception.
I changed the above line as
char[] a = new char[100]; and complied. It worked fine.
Thanks,
- 11-20-2007, 05:45 PM #4
Member
- Join Date
- Nov 2007
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Counting numbers up and down
By radio in forum New To JavaReplies: 4Last Post: 05-06-2011, 03:03 PM -
Counting Pixels
By shaungoater in forum Java 2DReplies: 5Last Post: 11-29-2007, 05:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks