Introduction
Problem will appear when the method——next and the nextLine used at the same time in the java source.
What is the next() and the nextLine() ?
Chinese key words
这里讨论的是关于java scanner类中的next方法和nextLine方法.
Problem
Firstly, let us see the code as follows——a simple example of input/output use method next only.
package cn.nileader.app.vi;
import java.util.Scanner;
public class Test {
public static void main(String[] args) throws Exception{
Scanner cin = new Scanner(System.in);
String ni = cin.nextLine();
System.out.print("用Scanner类的next()方法接收了你刚刚输入的字符串是:"+ni+" 长度是"+ni.length());
ni = cin.nextLine();
System.out.print("用Scanner类的next()方法接收了你刚刚输入的字符串是:"+ni+" 长度是"+ni.length());
}
}
Then,nextLine():
package cn.nileader.app.vi;
import java.util.Scanner;
public class Test {
public static void main(String[] args) throws Exception{
Scanner cin = new Scanner(System.in);
String ni = cin.next();
System.out.print("用Scanner类的next()方法接收了你刚刚输入的字符串是:"+ni+" 长度是"+ni.length());
ni = cin.next();
System.out.print("用Scanner类的next()方法接收了你刚刚输入的字符串是:"+ni+" 长度是"+ni.length());
}
}
The codes above-mentioned are right but silly.But,when you run the follow code,you will have a problem.
package cn.nileader.app.vi;
import java.util.Scanner;
public class Test {
public static void main(String[] args) throws Exception{
Scanner cin = new Scanner(System.in);
String ni = cin.next();
System.out.println("用Scanner类的next()方法接收了你刚刚输入的字符串是:"+ni+" 长度是"+ni.length());
ni = cin.nextLine();
System.out.print("用Scanner类的next()方法接收了你刚刚输入的字符串是:"+ni+" 长度是"+ni.length());
}
}
Runtime result:
ni掌柜
用Scanner类的next()方法接收了你刚刚输入的字符串是:ni掌柜 长度是4
用Scanner类的next()方法接收了你刚刚输入的字符串是: 长度是0
you will find that when you use the next() and the nextLine() at the same time ,you have no chance to input the data which the nextLine() method will scan.
Analysis
To put it simply, differences between next method and the nextLine is that the former scan a continuous character while the nextLine scans all character in one line.
The reason why problem above-mentioned is that the non_responsible of the method next().
As the method work, it scan the line where there is a cursor,but stops when it meet some particular characte,such as space or a line changing sign,and cut the character that has scanned.
Let’s look into the result above-mentioned, the first method next() have scanned the character of ‘ni掌柜’, but it has not scanned the character of ‘\n’, what’s more, it has not been responsible for the line changing. For the result, it left the character of ‘\n’ to nextLine() for executing.
As we know that , “only the money appear will the method nextLine() open its eye”, so ,nextLine() will stop scanning as soon as it has scanned the character of ‘\n’ , and there are the characters which in before the ‘/n’ in that line, that is,an empty character. Then, problem appears.
Solution
package cn.nileader.app.vi;
import java.util.Scanner;
public class Test {
public static void main(String[] args) throws Exception{
Scanner cin = new Scanner(System.in);
String ni = cin.next();
System.out.println("用Scanner类的next()方法接收了你刚刚输入的字符串是:"+ni+" 长度是"+ni.length());
while( (ni = cin.nextLine()).equals("") ){}
System.out.print("用Scanner类的next()方法接收了你刚刚输入的字符串是:"+ni+" 长度是"+ni.length());
}
}
Test result:
ni掌柜
用Scanner类的next()方法接收了你刚刚输入的字符串是:ni掌柜 长度是4
是个天才
用Scanner类的next()方法接收了你刚刚输入的字符串是:是个天才 长度是4
just use the program “while( (ni = cin.nextLine()).equals("") ){}” to replace the “ cin.nextLine() ”