2017-11-02 16:33:11
Scanner class : A simple text scanner that can use regular expressions to parse basic types and strings.
Scanner
Breaks its input into tokens using the delimiter mode, which, by default, matches whitespace. You can then use a different next method to convert the resulting token to a different type of value.
* Construction Method
* Common Methods
Basic Format: hasnextxxx (): Determine if there is a next entry, where Xxx can be int,double and so on. If you need to decide whether to include the next string , you can omit Xxx.
nextxxx (): Gets the next entry.
By default , scanner uses spaces and carriage returns as delimiters.
Commonly used is the public int Nextint (), the public String nextline ().
There is a problem getting the numeric type first, and then getting the string type. This problem has also been encountered in C + +.
public static void Main (string[] args) { Scanner sc = new Scanner (system.in); Y will be empty int x=sc.nextint (); String y=sc.nextline (); System.out.println (x+y); }
Workaround:
A: Redefine a scanner
B: Use String to get all first, then convert
public static void Main (string[] args) { Scanner sc = new Scanner (system.in); Y will be empty int x=sc.nextint (); Scanner SC2 = new Scanner (system.in); String y=sc2.nextline (); System.out.println (x+y); }
Java Common Object-scanner class