in the implementation of the character window input, many people prefer to use the scanner scanner, it is easier to operate. In the process of programming, I found that there are two ways to implement string input with scanner, one is next (), the other is nextline (), but what is the difference between the two methods? I looked up some information to sum up, I hope to help you ~
Next () Be sure to read the valid characters before you can end the input. The next () method automatically removes the SPACEBAR, the TAB key, or the ENTER key that is encountered before a valid character is entered, and the next () method will only be entered after a valid character, and then the space bar, The TAB key or enter key is treated as a separator or terminator.
Simply put, next () finds and returns the next complete tag from this scanner. The full tag is preceded by 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, that is, the nextline () method returns all the characters before the ENTER key, and it can get a string with a space.
Given the differences between the two methods, it is important to note the combination of the next () method and the Nextline () method, for example:
<span style= "font-family:fangsong_gb2312;" >im</span>port Java.util.Scanner;
public class nexttest{public
static void Main (string[] args) {
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 ("The string entered is:" +s1+ "" +s2);
}
Run Result:
Please enter the first string: Home
Please enter a second string: work
The string you entered is: Home work
But if you change the program:
S1=sc.next ();
S2=sc.nextline ();
the results of the operation are:
Please enter the first string: Home
Enter a second string: The string you entered is: home
As you can see, nextline () automatically reads the Enter removed by next () as his terminator, so there's no way to give S2 a value from the keyboard. After verification, I found this problem with other next methods, such as Double nextdouble (), float nextfloat (), int nextint () and nextline (), and the solution was: at each next (), Nextdouble (), Nextfloat (), Nextint (), and so on, add a nextline () statement that filters out the Enter terminator that is removed by next ().
the above program is rewritten as:
Import Java.util.Scanner;
public class Nexttest {
<span style= "White-space:pre" > </span>public static void Main (string[) args) {
<span style= "White-space:pre" > </span>string s1,s2;
<span style= "White-space:pre" > </span>scanner sc=new Scanner (system.in);
<span style= "White-space:pre" > </span>system.out.print ("Please enter the first string:");
<span style= "White-space:pre" > </span>s1=sc.next ();
<span style= "White-space:pre" > </span>sc.nextline ();
<span style= "White-space:pre" > </span>system.out.print ("Please enter a second string:");
<span style= "White-space:pre" > </span>s2=sc.nextline ();
<span style= "White-space:pre" > </span>system.out.println ("The input string is:" +s1+ "" +s2);
<span style= "White-space:pre" > </span>}
The results of the operation are:
Please enter the first string: Home
Please enter a second string: work
The string you entered is: Home work