Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Query about Use of nextLine() method of Scanner class

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Query about Use of nextLine() method of Scanner class

    My code is as follows:

    import java.util.Scanner;
    public class GetUserInput {
    public static void main (String args[]) {
    int i;
    float f ;
    String s;

    Scanner input = new Scanner (System.in);



    System.out.println ("enter an integer: ");
    i = input.nextInt();
    System.out.println ("you entered an integer equal to " + i);

    System.out.println ("enter a floating point number: ");
    f = input.nextFloat();
    System.out.println ("You entered a floating point number equal to " + f );

    System.out.println ("enter a sentence: ");
    s = input.nextLine();
    System.out.println ("You entered a sentence which is \" " +s+ " \" ");

    }
    }


    Output:

    D:\Java Codebase>javac GetUserInput.java

    D:\Java Codebase>java GetUserInput
    enter an integer:
    2
    you entered an integer equal to 2
    enter a floating point number:
    7
    You entered a floating point number equal to 7.0
    enter a sentence:
    You entered a sentence which is " "



    My Query is: input.nextLine() does not wait for user input. Instead it continues execution from next line. But if I move up input.nextLine(); before both input.nextInt(); and input.nextFloat() in the above code, the execution works fine, input.nextLine(); waits for user input. edited code and output are as follows.

    import java.util.Scanner;
    public class GetUserInput {
    public static void main (String args[]) {
    int i;
    float f ;
    String s;

    Scanner input = new Scanner (System.in);

    System.out.println ("enter a sentence: ");
    s = input.nextLine();
    System.out.println ("You entered a sentence which is \" " +s+ " \" ");

    System.out.println ("enter an integer: ");
    i = input.nextInt();
    System.out.println ("you entered an integer equal to " + i);

    System.out.println ("enter a floating point number: ");
    f = input.nextFloat();
    System.out.println ("You entered a floating point number equal to " + f );

    }
    }


    output:

    D:\Java Codebase>javac GetUserInput.java

    D:\Java Codebase>java GetUserInput
    enter a sentence:
    hi
    You entered a sentence which is " hi "
    enter an integer:
    1
    you entered an integer equal to 1
    enter a floating point number:
    8
    You entered a floating point number equal to 8.0


    Clarifications Invited.

    Thanks in advance,
    Satya
    Last edited by satyajit; April 10th, 2014 at 08:53 AM. Reason: Solved.


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Query about Use of nextLine() method of Scanner class

    When you enter a numeric value and use nextInt() you have entered an Integer and carriage return. nextInt reads the integer and the CR is still in the buffer. Now you call nextLine which reads the old CR form the buffer. Your first class will work if you insert input.next() before reading the sentence.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Query about Use of nextLine() method of Scanner class

    Thanks PhHein! That clarified my doubt in every aspect, great explanation. :-)

Similar Threads

  1. scanner.nextLine()
    By R0bsterz in forum Java Theory & Questions
    Replies: 13
    Last Post: April 25th, 2013, 06:26 AM
  2. Dont quite understand how Scanner.nextLine() works
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 14
    Last Post: September 21st, 2011, 05:27 PM
  3. Question concerning nextLine() method in Scanner Class
    By r_sardinas in forum Java SE APIs
    Replies: 2
    Last Post: October 20th, 2010, 08:35 PM
  4. problem with Scanner nextLine() method
    By mia_tech in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 10th, 2009, 04:14 AM

Tags for this Thread