What is discussed here is about the next method and nextLine method in the java scanner class. What are next() and nextLine() methods?
The problem occurs:
Let’s look at the following code first: A simple console input and output:
view plaincopy to clipboardprint?
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("Use the next() method of the Scanner class to receive the string you just entered: "+ni+" and the length is "+ni.length());
ni = cin.nextLine();
System.out.print("Use the next() method of the Scanner class to receive the string you just entered: "+ni+" and the length is "+ni.length());
}
}
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("Use the next() method of the Scanner class to receive the string you just entered: "+ni+" and the length is "+ni.length());
ni = cin.nextLine();
System.out.print("Use the next() method of the Scanner class to receive the string you just entered: "+ni+" and the length is "+ni.length());
}
}
Look at the following code again:
view plaincopy to clipboardprint?
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("Use the next() method of the Scanner class to receive the string you just entered: "+ni+" and the length is "+ni.length());
ni = cin.next();
System.out.print("Use the next() method of the Scanner class to receive the string you just entered: "+ni+" and the length is "+ni.length());
}
}
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("Use the next() method of the Scanner class to receive the string you just entered: "+ni+" and the length is "+ni.length());
ni = cin.next();
System.out.print("Use the next() method of the Scanner class to receive the string you just entered: "+ni+" and the length is "+ni.length());
}
}
The above code snippets are all correct and boring.
The wonderful thing is coming...
Run the following code:
view plaincopy to clipboardprint?
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("Using the next() method of the Scanner class to receive the string you just entered is: "+ni+" and the length is "+ni.length());
ni = cin.nextLine();
System.out.print("Use the next() method of the Scanner class to receive the string you just entered: "+ni+" and the length is "+ni.length());
}
}
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("Using the next() method of the Scanner class to receive the string you just entered is: "+ni+" and the length is "+ni.length());
ni = cin.nextLine();
System.out.print("Use the next() method of the Scanner class to receive the string you just entered: "+ni+" and the length is "+ni.length());
}
}
The running results are as follows:
shopkeeper
Use the next() method of the Scanner class to receive the string you just entered: ni shopkeeper, the length is 4
Use the next() method of the Scanner class to receive the string you just entered: the length is 0
It's over, when the next and nextLine methods of Java Scanner are used together,
You have no chance to enter the second string! ! !
Problem analysis:
The apparent difference between the next method and the nextLine method is that the former reads a "segment" of characters, while the latter reads a line of strings.
Simply put, the reason for the above phenomenon is mainly caused by the irresponsibility of the next() method.
How the next() method works: (superficially speaking)
Scan from a line until you encounter a specific identifier: a space or a newline character (n), and get the character (string) before it.
Look here, the first next() method reads "ni shopkeeper", but does not read "n", nor is it responsible for line breaks, and then throws "n" to nextLine() for processing.
As you know, nextLine() belongs to the kind of "money-seeing" type that accepts "n" when it sees it, and then scans the content before n, which is an empty string. Then something went wrong.
Problem solving:
view plaincopy to clipboardprint?
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("Using the next() method of the Scanner class to receive the string you just entered is: "+ni+" and the length is "+ni.length());
while( (ni = cin.nextLine()).equals("") ){}
System.out.print("Use the next() method of the Scanner class to receive the string you just entered: "+ni+" and the length is "+ni.length());
}
}
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("Using the next() method of the Scanner class to receive the string you just entered is: "+ni+" and the length is "+ni.length());
while( (ni = cin.nextLine()).equals("") ){}
System.out.print("Use the next() method of the Scanner class to receive the string you just entered: "+ni+" and the length is "+ni.length());
}
}
Running results:
shopkeeper
Use the next() method of the Scanner class to receive the string you just entered: ni shopkeeper, the length is 4
is a genius
Use the next() method of the Scanner class to receive the string you just entered: He is a genius and the length is 4
Put the original second cin.nextLine()
Change to while( (ni = cin.nextLine()).equals("") ){}
In this way, the influence of "n" in the previous line can be eliminated.