I personally prefer to use the scanner scanner when I implement the input of the character window, which is relatively simple to operate. In the process of writing, I found that there are two ways to implement string input using scanner, one is next () and one nextline (), but what is the difference between these two methods? I looked up some information to summarize, I hope to be helpful to everyone ~
Next () must be read to a valid character before the end of the input, to enter a valid character before the space bar, tab or enter key, such as The Terminator, the next () method will automatically remove it, only after entering a valid character, the next () method will be entered after the space bar, The TAB key or enter key is treated as a delimiter or terminator. Simply put, next () finds and returns the next complete tag from this scanner. Before and after the complete tag is the input information that matches the delimited pattern, so the next method cannot get a string with spaces.
The Terminator of the Nextline () method is just the ENTER key, which means that the nextline () method returns all characters before the ENTER key, which is a string with spaces.
As long as the difference between the two methods, students must pay attention to next () method and Nextline (0 method of the used, the following example to illustrate:
Import Java.util.Scanner;
public class Nexttest {
public static void Main (string[] args) {
TODO auto-generated Method Stub
String s1,s2;
Scanner sc=new Scanner (system.in);
System.out.print ("Please enter the first string:");
S1=sc.nextline ();
System.out.print ("Please enter a second string:");
S2=sc.next ();
System.out.println ("Input string is:" +s1+ "" "+s2);
}
}
Operation Result:
Please enter the first string: Home
Please enter a second string: work
The string entered is: Home work
But if the program is changed, S1=sc.next (); S2=sc.nextline ();
The operating result is:
Please enter the first string: Home
Please enter a second string: The string entered is: Home
As you can see,nextline () automatically reads the Enter that was removed by next () as his terminator, so there is no way to enter a value for S2 from the keyboard. After verification, I found that the other next method, such as Double nextdouble (), float nextfloat (), int nextint (), and so on with the nextline () with the problem, the solution is: in each next (), After statements such as Nextdouble (), Nextfloat (), Nextint (), and a nextline () statement, the Enter Terminator removed by next () is filtered out. for example, the above program is rewritten as:
Import Java.util.Scanner;
public class Nexttest {
public static void Main (string[] args) {
TODO auto-generated Method Stub
String s1,s2;
Scanner sc=new Scanner (system.in);
System.out.print ("Please enter the first string:");
S1=sc.next ();
Sc.nextline ();
System.out.print ("Please enter a second string:");
S2=sc.nextline ();
System.out.println ("Input string is:" +s1+ "" "+s2);
}
}
The operating result is:
Please enter the first string: Home
Please enter a second string: work
The string entered is: Home work
Scanner next () and nextline () read string methods and differences