Scanner.nextLine() need double enter to finish a line in Java6

Viewed 352

I have problem in Java6 when using Scanner.nextLine(). The code is simply below

package test; 

import java.io.InputStreamReader; 
import java.io.Reader; 
import java.util.Scanner; 

public class TestScanner 
{ 

        public static void main(String[] args) 
        { 
                Reader in = new InputStreamReader(System.in); 

                Scanner input = new Scanner(in); 

                while (true) 
                { 
                        String line = input.nextLine(); 

                        if ("exit".equals(line)) 
                        { 
                                break; 
                        } 
                        else 
                        { 
                                System.out.println(">>" + line); 
                        } 
                } 
        } 

}

In Java6, I need ENTER twice to finish line and then make output ">>XXX", like

Input:

abc
(first ENTER here)
(second ENTER here)

Output:

>>abc

The JVM info is below:

java version "1.6.0" 
Java(TM) SE Runtime Environment (build pap3260sr16fp41-20170215_04(SR16 FP41)) 
IBM J9 VM (build 2.4, JRE 1.6.0 IBM J9 2.4 AIX ppc-32 jvmap3260sr16fp40-20161215_329869 (JIT enabled, AOT enabled) 
J9VM - 20161215_329869 
JIT  - r9_20160630_120368 
GC   - GA24_Java6_SR16_20161215_0927_B329869) 
JCL  - 20170215_01

On the other hand, in Java5, I run the same code and only need ENTER once will do. The Java5 VM info is below:

java version "1.5.0" 
Java(TM) 2 Runtime Environment, Standard Edition (build pap64devifx-20151009 (SR16 FP14 )) 
IBM J9 VM (build 2.3, J2RE 1.5.0 IBM J9 2.3 AIX ppc64-64 j9vmap6423ifx-20150401 (JIT enabled) 
J9VM - 20150323_240985_BHdSMr 
JIT  - 20130920_46470ifx2_r8 
GC   - 20141118_AA) 
JCL  - 20151009

Furthermore, I make some other inputs in Java6 below, and find it can not recognize the line terminator in correct order?

abc(input and ENTER)
efg(input and ENTER)
>>abc(got output here)
hij(keep input and ENTER)
>>efg(got output which should output above)

But still correct in Java5 like below

abc(input and ENTER)
>>abc(got output)
efg(input and ENTER)
>>efg(got output)
hij(input and ENTER)
>>hij(got output)

What makes the difference? And what can I do to make its behavior in Java6 same to Java5?

0 Answers
Related