Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By sunde887

Thread: Problem with scanning char

  1. #1
    danthegreat is offline Member
    Join Date
    Sep 2011
    Location
    Washington DC
    Posts
    51
    Rep Power
    0

    Default Problem with scanning char

    Excerpt of my program:
    Java 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)
    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 debug

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    danthegreat is offline Member
    Join Date
    Sep 2011
    Location
    Washington DC
    Posts
    51
    Rep Power
    0

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

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Problem with scanning char

    Quote Originally Posted by danthegreat View Post
    This is the full program:
    Well I guess it was a good guess on my part!

    You do in fact call nextInt() prior to your code above:


    Java Code:
    		System.out.print("Enter Quantity: ");
    		int quantity = Keyboard.nextInt();
    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();
    		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.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default 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 :(
    Fubarable likes this.

Similar Threads

  1. Replies: 11
    Last Post: 08-05-2011, 09:23 PM
  2. problem with reading a text file with special char
    By kishan.java in forum New To Java
    Replies: 1
    Last Post: 04-10-2011, 10:30 AM
  3. char problem.
    By santa in forum New To Java
    Replies: 5
    Last Post: 02-18-2011, 01:24 AM
  4. replaceALL(char oldChar, char newChar) method
    By arson09 in forum New To Java
    Replies: 0
    Last Post: 04-28-2010, 06:48 AM
  5. Problem with if statement - Char
    By KardKaper in forum New To Java
    Replies: 8
    Last Post: 12-22-2009, 02:14 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
  •