Java Scanner Class
java.util.Scanner is a new feature in Java 5. We can obtain user input through the Scanner class.
The following is the basic syntax for creating a Scanner object:
Next, we demonstrate a simple data input, and obtain the input string through the next() and nextLine() methods of the Scanner class. Before reading, we generally need to use hasNext and hasNextLine to determine whether there is still input data:
Using the next method:
ScannerDemo.java file code:
The output after executing the above program is:
$ javac ScannerDemo.java $ java ScannerDemo next方式接收: example com 输入的数据为:example
It can be seen that the com string was not output. Next we look at nextLine.
Using the nextLine method:
ScannerDemo.java file code:
The output after executing the above program is:
$ javac ScannerDemo.java $ java ScannerDemo nextLine方式接收: example com 输入的数据为:example com
It can be seen that the com string is output.
Difference between next() and nextLine()
next():
- 1. Input can only be terminated after reading valid characters.
- 2. For whitespace encountered before valid input characters, the next() method automatically removes it.
- 3. Only after entering valid characters will the whitespace after them be used as a separator or terminator.
- next() cannot get a string with spaces.
nextLine():
- 1. Enter is the terminator, that is, the nextLine() method returns all characters before the Enter key is input.
- 2. It can obtain whitespace.
If you want to enter data of type int or float, the Scanner class also supports it, but before input, it is best to use the hasNextXxx() method to validate, then use nextXxx() to read:
ScannerDemo.java file code:
The output after executing the above program is:
$ javac ScannerDemo.java $ java ScannerDemo 输入整数:12 整数数据:12 输入小数:1.2 小数数据:1.2
In the following example, we can enter multiple numbers and calculate their sum and average. Each number is confirmed by pressing Enter. End input by entering a non-number, then output the execution result:
ScannerDemo.java file code:
The output after executing the above program is (enter a non-number to end input):
$ javac ScannerDemo.java $ java ScannerDemo 请输入数字: 12 23 15 21.4 end 4个数的和为71.4 4个数的平均值是17.85
Common methods
| Method | Description |
|---|---|
| Constructors | |
Scanner(File source) |
Create Scanner from a file |
Scanner(InputStream source) |
Create Scanner from an input stream |
Scanner(String source) |
Create Scanner from a string |
| Basic input methods | |
boolean hasNext() |
Check if there is a next token (separated by spaces) |
String next() |
Read the next token (string) |
boolean hasNextLine() |
Check if there is a next line |
String nextLine() |
Read the content of the next line |
| Type checking methods | |
boolean hasNextInt() |
Check if the next token is an integer |
boolean hasNextDouble() |
Check if the next token is a double |
boolean hasNextBoolean() |
Check if the next token is a boolean |
| Type reading methods | |
int nextInt() |
Read the next integer |
double nextDouble() |
Read the next double |
boolean nextBoolean() |
Read the next boolean |
long nextLong() |
Read the next long integer |
float nextFloat() |
Read the next float |
short nextShort() |
Read the next short integer |
byte nextByte() |
Read the next byte |
| Delimiter control | |
Scanner useDelimiter(String pattern) |
Set the delimiter pattern |
Scanner useDelimiter(Pattern pattern) |
Set the delimiter using a regex |
String delimiter() |
Return the currently used delimiter pattern |
| Other methods | |
void close() |
Close the scanner |
Scanner skip(Pattern pattern) |
Skip input matching the specified pattern |
Scanner skip(String pattern) |
Skip input matching the specified string |
String findInLine(Pattern pattern) |
Find the specified pattern in the current line |
String findInLine(String pattern) |
Find the specified string in the current line |
Scanner reset() |
Reset the scanner |
Locale locale() |
Return the locale currently used by the scanner |
Scanner useLocale(Locale locale) |
Set the scanner's locale |
For more content, refer to the API documentation:https://www.example.com/manual/jdk11api/java.base/java/util/Scanner.html。
Other extensions