Results 1 to 4 of 4
  1. #1
    Cass29 is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Default input.nextLine();

    Hi everyone hope someopne can help me. I'm writing a code that holds a clients details and I want to check if the right details are displayed. When I prompt the user to enter the new details I'm getting two line diplayed as one. Here is the section of code I have

    public void checkDetails()
    {

    int correct;

    Scanner input = new Scanner(System.in);

    System.out.println( "Are the Details Displayed Above Correct? (Please Enter 1 for Yes and 2 for No" );
    correct = input.nextInt();

    System.out.println();

    while( correct == 2 )
    {
    System.out.println("Please Enter Clients Name:");
    clientName = input.nextLine();

    System.out.println("Please Enter Clients Street Address:");
    clientStreet = input.nextLine();

    System.out.println("Please Enter Clients Town:");
    clientTown = input.nextLine();

    System.out.println("Please Enter Clients State:");
    clientState = input.nextLine();

    System.out.println("Please Enter Clients Budget:");
    clientBudget = input.nextInt();

    }
    }

    The problem is that Enter Clients Name and Enter Clients Street
    comes out together without a space to enter the name so whatever I enter for name is stored as the stree address.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    If you type '2' (for incorrect details) note that you actually type <2> <enter> so two chars are present in the input buffer; the nextInt() method reads the <2>, takes it from the buffer and converts it to an int but it leaves the <enter> in the input buffer (because its not part of an int representation). Next you attempt to read an entire line, that method finds the <enter> character in the input buffer and duly decides that you have typed an empty line and returns.

    You have to get rid of that <enter> key in the input buffer; simply read a line by calling the nextLine() method (and discard its return value) before you want to read a client's name.

    kind regards,

    Jos

  3. #3
    renamed is offline Member
    Join Date
    Dec 2009
    Location
    Rio de Janeiro
    Posts
    38
    Rep Power
    0

    Default

    Hi Cass29 welcome to the forum.

    1. Please enter your code between the tags "code", the "#" symbol that appears when you type the text of your post. :D

    2. Please avoid using nextInt(); nextDouble(); nextFloat(); etc methods because they do not recognize "enter" as part of what you wrote (since "enter" is not a number). That's a problem because "enter" still remains in buffer and when you try to read another information from the keyboard, it skips this information.
    Use nextLine() instead.

    Change:
    Java Code:
    correct = input.nextInt();
    for

    Java Code:
    correct = Integer.parseInt(input.nextLine());
    3. You have boolean type value in Java. Try to use it.

    Java Code:
                    int choice;
    		boolean correct;
    
    		Scanner input = new Scanner(System.in);
    
    		System.out.println( "Are the Details Displayed Above Correct? (Please Enter 1 for Yes and 2 for No" );
    		choice = Integer.parseInt(input.nextLine());
    
    		correct = (choice == 2);
    		System.out.println();
    
    		while( correct )
    		{
    4. You never change the value of your while condition. That will result in an infinitive loop.
    Please don't laugh at my English... I'm trying my best! :)

  4. #4
    Cass29 is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Default

    Thanks for the help guys, the code is working perfectly now.

Similar Threads

  1. .nextLine(); only picks up first word
    By ethanemc505 in forum New To Java
    Replies: 1
    Last Post: 10-08-2009, 07:30 PM
  2. Input help
    By rice in forum New To Java
    Replies: 6
    Last Post: 09-26-2009, 07:07 PM
  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. Input....in JTable
    By kirtesh4u in forum AWT / Swing
    Replies: 0
    Last Post: 11-15-2008, 09:15 AM
  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
  •