Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-19-2007, 11:34 PM
Member
 
Join Date: Nov 2007
Posts: 2
MattN is on a distinguished road
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:

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; } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-19-2007, 11:51 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-20-2007, 12:52 PM
Member
 
Join Date: Aug 2007
Posts: 22
revathi17 is on a distinguished road
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,
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-20-2007, 07:45 PM
Member
 
Join Date: Nov 2007
Posts: 2
MattN is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Counting numbers up and down radio New To Java 3 12-01-2007 09:08 PM
Counting Pixels shaungoater Java 2D 5 11-29-2007 07:51 PM


All times are GMT +3. The time now is 03:52 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org