The Scanner class encapsulates the input stream object and gets the entire line of text characters entered by the user using the Nextline () method input stream
Package hahaa;
Import Java.util.Scanner;
public class Ha {
public static void Main (string[] args) {
Scanner Scanner =new Scanner (system.in);
System.out.println ("Please enter the number you want to enter");
String line = Scanner.nextline ();
System.out.println ("The length of the output number" +line.length ());
}
}
Control Desk
Please enter the number you want to enter
2315348
The length of the output number 7
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.
After each statement of next (), Nextdouble (), Nextfloat (), Nextint (), plus a nextline () statement, the next () will remove the space, including the space, until the text entered after enter is valid.
Main function (for reference)
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.next ();
Sc.nextline ();
System.out.print ("Please enter a second string:");
S2=sc.next ();
System.out.println ("Input string is:" +s1+ "" "+s2);
}
Console----
Please enter the first string: Woini DASDASFA
Please enter a second string: ADSDFSDASD
The string entered is: Woini Adsdfs
Java Self-study note-Basic Syntax 2-receive input characters and scanner.nextline from the console