Results 1 to 5 of 5
Thread: Problem with scanning char
- 09-09-2011, 06:16 PM #1
Member
- Join Date
- Sep 2011
- Location
- Washington DC
- Posts
- 51
- Rep Power
- 0
Problem with scanning char
Excerpt of my program:
I get that error stating that my string index is out of range? How is that possible? if the user enters 'y' then shouldn't that be at a string index of 0? Eclipse doesn't let me enter in anything at all when I debugJava Code:System.out.print("Next customer (y/n): "); again=Keyboard.nextLine().charAt(0); if(again=='y'){ num=1; } else if(again=='n'){ System.out.print("Thank you for using Floppy Systems."); System.exit(-1); } }while(num==1); Next customer (y/n): Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.lang.String.charAt(String.java:686) at DanielHello.main(DanielHello.java:27)
-
Re: Problem with scanning char
It's hard to say what the problem is based on the information at hand, but this suggests that your String has no characters in it. Perhaps prior to this code block, you've used the Scanner object to get a next() or nextInt() and did not also take care to swallow the end of line token with a nextLine() call, but I'm just guessing.
- 09-09-2011, 06:45 PM #3
Member
- Join Date
- Sep 2011
- Location
- Washington DC
- Posts
- 51
- Rep Power
- 0
Re: Problem with scanning char
This is the full program:
Java Code:import java.util.*; public class DanielHello { public static void main(String[] args){ final double unit = .26; Scanner Keyboard = new Scanner(System.in); char again; int num = 0; do{ System.out.print("Enter Quantity: "); int quantity = Keyboard.nextInt(); if(quantity%25==0){ System.out.println("You have ordered " + quantity + " Floppies --- $" + quantity*unit); } else if(quantity%25!=0){ System.out.println("Floppies can only be ordered in packs of 25."); } System.out.print("Next customer (y/n): "); again=Keyboard.nextLine().charAt(0); if(again=='y'){ num=1; } else if(again=='n'){ System.out.print("Thank you for using Floppy Systems."); System.exit(-1); } }while(num==1); } }
-
Re: Problem with scanning char
Well I guess it was a good guess on my part!
.gif)
You do in fact call nextInt() prior to your code above:
and you don't handle the end of line token which is messing up your scanner. The solution is to handle that token with a nextLine() call:Java Code:System.out.print("Enter Quantity: "); int quantity = Keyboard.nextInt();
Java Code:System.out.print("Enter Quantity: "); int quantity = Keyboard.nextInt(); Keyboard.nextLine(); // swallow the end of line token
Also, you'll not want to capitalize the first letter of variables but only classes, interfaces, enums and the like so as not to confuse those helping you or grading you.
- 09-09-2011, 07:01 PM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: Problem with scanning char
As Fubar eluded to, you called nextInt and then nextLine, which causes problems due to not swalling the eol. Two suggestions:
1) Add Keyboard.nextLine(); after calls to nextInt(), nextDouble() and similar methods.
2) Only read lines at a time and parse the number with Integer.parseInt(String s)
int quantity = Integer.parseInt(Keyboard.nextLine());
Either approach will work well enough.
Edit: Too slow :(
Similar Threads
-
Problem encountered When the char '*' is passed as command line argument-Please help!
By omripe in forum Advanced JavaReplies: 11Last Post: 08-05-2011, 08:23 PM -
problem with reading a text file with special char
By kishan.java in forum New To JavaReplies: 1Last Post: 04-10-2011, 09:30 AM -
char problem.
By santa in forum New To JavaReplies: 5Last Post: 02-18-2011, 12:24 AM -
replaceALL(char oldChar, char newChar) method
By arson09 in forum New To JavaReplies: 0Last Post: 04-28-2010, 05:48 AM -
Problem with if statement - Char
By KardKaper in forum New To JavaReplies: 8Last Post: 12-22-2009, 01:14 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks