使用Scanner.nextLine()时,我在Java6中遇到问题。 代码就在
之下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);
}
}
}
}
在Java6中,我需要两次ENTER来完成整行,然后输出">> XXX",如
输入:
abc (first ENTER here) (second ENTER here)
输出:
>>abc
JVM信息如下:
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
另一方面,在Java5中,我运行相同的代码,只需要输入一次。 Java5 VM信息如下:
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
此外,我在下面的Java6中做了一些其他输入,发现它无法正确识别行终止符?
abc(input and ENTER) efg(input and ENTER) >>abc(got output here) hij(keep input and ENTER) >>efg(got output which should output above)
但在Java5中仍然正确如下
abc(input and ENTER) >>abc(got output) efg(input and ENTER) >>efg(got output) hij(input and ENTER) >>hij(got output)
有什么区别?我怎么做才能使它在Java6中的行为与Java5相同?