Results 1 to 4 of 4
  1. #1
    MattN is offline Member
    Join Date
    Nov 2007
    Posts
    2
    Rep Power
    0

    Default 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;
        }
        
        
    }

  2. #2
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    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.

  3. #3
    revathi17 is offline Member
    Join Date
    Aug 2007
    Posts
    26
    Rep Power
    0

    Default

    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,

  4. #4
    MattN is offline Member
    Join Date
    Nov 2007
    Posts
    2
    Rep Power
    0

    Default

    Thanks a Bunch!

    Once i changed it to 100, it worked great. Only problem then, because i did an else, it was counting everything else as a constonant, so i had 96 constonants 4 vowels, etc. So i had to do an Else If, and input every letter. But it works now! Thanks!

    Matt

Similar Threads

  1. Counting numbers up and down
    By radio in forum New To Java
    Replies: 4
    Last Post: 05-06-2011, 04:03 PM
  2. Counting Pixels
    By shaungoater in forum Java 2D
    Replies: 5
    Last Post: 11-29-2007, 06:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •