JDK-6351708 : unempty buffer makes Scanner.nextLine as if it is skipped!
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util
  • Affected Version: 5.0
  • Priority: P3
  • Status: Closed
  • Resolution: Not an Issue
  • OS: linux
  • CPU: x86
  • Submitted: 2005-11-17
  • Updated: 2010-04-02
  • Resolved: 2005-12-02
Description
FULL PRODUCT VERSION :
java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Both:
Linux: Red Hat 8, kernel: 2.4
Windows: Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
When I use the method Scanner.nextLine() after reading an integer, float or double,.. it behaves like it skip Scanner.nextLine().

Infact, it is not skipped but after reading an integer, float or double the new line character would still be exist in the buffer of Scanner. Thus, when a one write Scanner.nextLine() after that, it will read that new line character which makes it to stop and reads nothing besides that new line character.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
in the implementation of the following methods: Scanner.nextInt(), Scanner.nextFloat() and Scanner.nextDouble(), you must clear the buffer from the new line character entered by a user.
 

What I'm doing right now is that I wriet Scanner.nextLine() to read the new line character and then use Scanner.nextLine() again and assign it to a variable

ACTUAL -
Scanner.nextLine() is like to not be executed.
if you look at the example below:
user will have the chance to enter string1, num, string3 but not string2.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.util.Scanner;
public class test {
     public static void main (String args[]) {
            Scanner input = new Scanner(System.in);
            int num;
            String str;
            System.out.print("Enter string1: ");
            str = input.nextLine(); // -----------(1)------------
            System.out.print("Enter a number: ");
            num = input.nextInt();
            System.out.print("Enter string2: ");
            str = input.nextLine(); // -----------(2)------------
            System.out.print("Enter string3: ");
            str = input.nextLine(); // -----------(3)------------
     }
}  
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
import java.util.Scanner;
public class test {
     public static void main (String args[]) {
            Scanner input = new Scanner(System.in);
            int num;
            String str;
            System.out.print("Enter string1: ");
            str = input.nextLine(); // -----------(1)------------
            System.out.print("Enter a number: ");
            num = input.nextInt();// here user will enter an integer and new line character. The method nextInt() will read the number and assign it to num but the new line character will remain in the buffer.
            System.out.print("Enter string2: ");
            input.nextLine(); // This will read the new line character left in the buffer.
            str = input.nextLine(); // -----------(2)------------
            System.out.print("Enter string3: ");
            str = input.nextLine(); // -----------(3)------------
     }
}

Comments
EVALUATION nextInt, nextFloat, nextDouble and nextLine all work the way the spec says they should. No, nextInt/Fload/XYZ should not suck the remaining new line character(s) and the nextLine() must read in the remaining of the buffer.
02-12-2005