Results 1 to 3 of 3

Thread: Unable to input

  1. #1
    jaylimix is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default Unable to input

    Java Code:
    if(ans==1){
    
    			System.out.print("Amend item code: ");
    			String s1= input.nextLine();
    			book[choice].setItemCode(s1);
    Output:

    What do you want to amend?: 1
    Amend item code: -------------------------------------------------
    | Books Menu |
    -------------------------------------------------

    As you can see, I am unable to input after Amend item code. It jumps straight to the next statement. Please help here.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You haven't really shown enough code. Just guessing, but very possibly the problem occurs when you set the value of ans. If input is an instance of Scanner and you call nextInt(), type "1" and press the enter key then you will get the integer one returned and the scanner stream will be left pointing just before the newline. A subsequent nextLine() will, therefore, return immediately with an empty string.

    You can test this with some debugging code:

    Java Code:
    String s1= input.nextLine();
    [color=green]System.out.println("About to set item code for \"" + s1 + "\"");[/color]
    book[choice].setItemCode(s1);
    The solution in such cases is to call nextLine() and disregard the returned string immediately after calling nextInt().

  3. #3
    aviolently is offline Member
    Join Date
    Jun 2011
    Posts
    23
    Rep Power
    0

    Default

    i think you need a for loop on it...

    this happened many times to me!

Similar Threads

  1. Unable to compile...
    By DrakalDragon in forum New To Java
    Replies: 3
    Last Post: 05-14-2011, 12:32 AM
  2. unable to run a jar file.
    By Jagdeesh123 in forum New To Java
    Replies: 5
    Last Post: 12-07-2010, 10:41 AM
  3. Input technique for unknown lines of input
    By ducreative in forum New To Java
    Replies: 16
    Last Post: 09-23-2009, 09:26 AM
  4. Unable to compile
    By gapper in forum New To Java
    Replies: 2
    Last Post: 01-14-2008, 04:31 PM
  5. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 08:46 AM

Posting Permissions

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