Results 1 to 4 of 4
  1. #1
    codeAJ's Avatar
    codeAJ is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default Scanner - obtaining strings from console with nextLine

    Hello,

    I'm a Java newbie having trouble reading strings with spaces in from the console.

    Here is an example:

    Java Code:
    import java.util.Scanner;
    
    public class Test {
       public static void main(String[] args) {
          Scanner input = new Scanner(System.in);
    	  System.out.print("Enter double: ");
    	  double d = input.nextDouble();
    	  System.out.print("Enter string: ");
    	  String s = input.nextLine();
    	  System.out.println("String is " + s);
       }
    }

    This code skips obtaining the string and moves on to the last statement:

    C:\> java Test
    Enter double: 4
    Enter string: String is

    If I remove the section of code to obtain the double, the string is captured ok.

    What am I doing wrong?! Can someone please help? Many thanks!

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

    Default

    I believe you need to make this call before prompting for s,
    Java Code:
    input.nextLine();
    try adding that and see if it works as expected.

  3. #3
    codeAJ's Avatar
    codeAJ is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    I believe you need to make this call before prompting for s,
    Java Code:
    input.nextLine();
    try adding that and see if it works as expected.
    Thanks, that did the trick. :D

    I don't know why, but it works!

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    I don't know why
    Scanner.netxInt, nextDouble etc consume the next matching token (characters that can be interpreted as int, double etc) and leave the rest in the Scanner. that 'rest' includes the EOL (end of line) generated when you press <Enter>

    The first nextLine encountered consumes this EOL without waiting for further input, as there is a 'next line' already available.

    db

Similar Threads

  1. Problem Of Scanner Object with its method nextLine()
    By Cluster Storm in forum AWT / Swing
    Replies: 12
    Last Post: 06-17-2010, 05:40 PM
  2. Replies: 13
    Last Post: 05-16-2010, 12:09 AM
  3. Using Scanner console for Graphics
    By bensewards in forum New To Java
    Replies: 3
    Last Post: 03-17-2010, 02:16 AM
  4. Reading a line from console using Scanner class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-18-2008, 11:52 AM
  5. Read from console (Scanner Class)
    By hey in forum New To Java
    Replies: 10
    Last Post: 12-11-2007, 10:11 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
  •