Results 1 to 10 of 10
  1. #1
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Simple user input not working

    A seemingly simple task has thrown up a strange error. Ive got a while loop which keeps adding assignments until the user decides they have finished. When i run it, it skips the module title section and just puts the module identifier on the same line, an example output below.

    Enter new assignment details
    ****************************
    Module Titile :> Module Identifier :> test
    Assignment Titile :> test

    Everything else prints out as it should, i dont understand why though as they are all the same . . .

    Code is below,

    Java Code:
    boolean add = true;
            while(add)
            {
                System.out.println("\nEnter new assignment details");
                System.out.println("****************************");
                
                System.out.print("Module Titile :> ");
                String mTitle = in.nextLine();
                System.out.print("Module Identifier :> ");
                String mIdentifier = in.nextLine();
                System.out.print("Assignment Titile :> ");
                String aTitle = in.nextLine();
                System.out.print("Date Set :> ");
                String dateSet = in.nextLine();
                System.out.print("Due Date :> ");
                String dueDate = in.nextLine();
                System.out.print("Assignment Author :> ");
                String aAuthor = in.nextLine();
                System.out.print("Weighting :> ");
                String weighting = in.nextLine();
                
                System.out.println("\nEnter another assignment? (y/n)");
                String another = in.next();
                if(another.equals("n"))
                    add = false;
           
            }

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Simple user input not working

    Did you make sure "in" is empty at the beginning? Initialize or clear it and show us the declaration please.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Simple user input not working

    Is there anything else happening before this?
    For example, is there a "Please choose your action" followed by some integer input?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Simple user input not working

    Quote Originally Posted by Tolls View Post
    Is there anything else happening before this?
    For example, is there a "Please choose your action" followed by some integer input?
    Its a console menu so before this there is a series of options and then the scanner reads the input as an int value. That then brings you to this menu to add an assignment.

  5. #5
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Simple user input not working

    I don't like being ignored...
    I like likes!

  6. #6
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Simple user input not working

    Sorry
    The scanner is declared at the start of the class Scanner in = new Scanner(System.in);
    It is then used in createMenu() to read an int as i said previously. I then use the scanner again in the above code to read a number of strings.
    Ive never had to clear the scanner before using it again in similar programs, how would i do that?

  7. #7
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Simple user input not working

    I would say
    while (in.hasNext()) in.next();

    But this is purely theoretical as I never worked with the scanner before, so I do not know if that is a blocking operation for the user if using a keyboard as input. :)
    I like likes!

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Simple user input not working

    Quote Originally Posted by wdh321 View Post
    Its a console menu so before this there is a series of options and then the scanner reads the input as an int value. That then brings you to this menu to add an assignment.
    Scanner.nextInt() will result in the newline character being left in the buffer.
    This character is then read when you use nextLine(), resulting in an empty String for that read.

    This is what is happening here.
    Stick a call to nextLine() prior to running this bit of code here (probably just after you've finished with the previous menu would be a good spot). This will have the effect Sierra talks about in their first post of clearing the buffer.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Simple user input not working

    Quote Originally Posted by Tolls View Post
    Scanner.nextInt() will result in the newline character being left in the buffer.
    This character is then read when you use nextLine(), resulting in an empty String for that read.

    This is what is happening here.
    Stick a call to nextLine() prior to running this bit of code here (probably just after you've finished with the previous menu would be a good spot). This will have the effect Sierra talks about in their first post of clearing the buffer.
    Thank you, put a call to nextLine() and problem is solved.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Simple user input not working

    All the nextXXX() methods clear the buffer before reading except nextLine().
    I have no idea at all why.
    It's the cause of a large number of questions on this forum...:)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. User Input???
    By jonytek in forum New To Java
    Replies: 8
    Last Post: 01-13-2013, 02:52 PM
  2. user input on gui
    By JoePenguin in forum New To Java
    Replies: 5
    Last Post: 01-26-2012, 07:27 PM
  3. Replies: 5
    Last Post: 11-27-2010, 03:49 AM
  4. User input- Pop Up Box
    By dedachi in forum AWT / Swing
    Replies: 3
    Last Post: 03-23-2009, 04:47 AM
  5. Scanner Issues (User Input: Very Simple)
    By carlodelmundo in forum New To Java
    Replies: 8
    Last Post: 10-31-2008, 02:44 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
  •