Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Scanner Class

Java Scanner Class

java.util.Scanner is a new feature in Java 5; we can use the Scanner class to obtain user input.

The following is the basic syntax for creating a Scanner object:

Scanner

s

=

new

Scanner

(

System

.

in

)

;

Next, we demonstrate the simplest data input, and obtain the input string through the Scanner class's next() and nextLine() methods. Before reading, we generally use hasNext and hasNextLine to check whether there is still input data:

Using the next method:

ScannerDemo.java file code:

import

java

.

util

.

Scanner

;

public

class

ScannerDemo

{

public

static

void

main

(

String

[

]

args

)

{

Scanner

scan

=

new

Scanner

(

System

.

in

)

;

//

Receive data from the keyboard

//

Receive string using the next method

System

.

out

.

println

(

"

Receive using the next method:

"

)

;

//

Check whether there is more input

if

(

scan

.

hasNext

(

)

)

{

String

str1

=

scan

.

next

(

)

;

System

.

out

.

println

(

"

The input data is:

"

str1

)

;

}

scan

.

close

(

)

;

}

}

The output of the above program is:


$ javac ScannerDemo.java
$ java ScannerDemo
Next method reception:
runoob com
输入的数据为:runoob

You can see that the com string was not output; next, let us look at nextLine.

Using the nextLine method:

ScannerDemo.java file code:

import

java

.

util

.

Scanner

;

public

class

ScannerDemo

{

public

static

void

main

(

String

[

]

args

)

{

Scanner

scan

=

new

Scanner

(

System

.

in

)

;

//

Receive data from the keyboard

//

Receive string using the nextLine method

System

.

out

.

println

(

"

Receive using the nextLine method:

"

)

;

//

Check whether there is more input

if

(

scan

.

hasNextLine

(

)

)

{

String

str2

=

scan

.

nextLine

(

)

;

System

.

out

.

println

(

"

The input data is:

"

str2

)

;

}

scan

.

close

(

)

;

}

}

The output of the above program is:


$ javac ScannerDemo.java
$ java ScannerDemo
Receiving using nextLine method:
runoob com
输入的数据为:runoob com

You can see that the com string is output.

The difference between next() and nextLine()

next():

nextLine():

If you want to input int or float type data, the Scanner class also provides support, but before input it is best to first use the hasNextXxx() method to validate, then use nextXxx() to read:

ScannerDemo.java file code:

import

java

.

util

.

Scanner

;

public

class

ScannerDemo

{

public

static

void

main

(

String

[

]

args

)

{

Scanner

scan

=

new

Scanner

(

System

.

in

)

;

//

Receive data from the keyboard

int

i

=

0

;

float

f

=

0

.0

f

;

System

.

out

.

print

(

"

Input integer:

"

)

;

if

(

scan

.

hasNextInt

(

)

)

{

//

Check whether the input is an integer

i

=

scan

.

nextInt

(

)

;

//

Receive integer

System

.

out

.

println

(

"

Integer data:

"

i

)

;

}

else

{

//

Input incorrect information

System

.

out

.

println

(

"

The input is not an integer!

"

)

;

}

System

.

out

.

print

(

"

Input decimal:

"

)

;

if

(

scan

.

hasNextFloat

(

)

)

{

//

Check whether the input is a decimal

f

=

scan

.

nextFloat

(

)

;

//

Receive decimal

System

.

out

.

println

(

"

Decimal data:

"

f

)

;

}

else

{

//

Input incorrect information

System

.

out

.

println

(

"

The input is not a decimal!

"

)

;

}

scan

.

close

(

)

;

}

}

The output of the above program is:


$ javac ScannerDemo.java
$ java ScannerDemo
输入整数:12
整数数据:12
输入小数:1.2
小数数据:1.2

The following example allows us to input multiple numbers and calculate their sum and average; confirm each number with Enter, and end the input by entering a non-number, then output the execution result:

ScannerDemo.java file code:

import

java

.

util

.

Scanner

;

class

RunoobTest

{

public

static

void

main

(

String

[

]

args

)

{

System

.

out

.

println

(

"

Please input a number:

"

)

;

Scanner

scan

=

new

Scanner

(

System

.

in

)

;

double

sum

=

0

;

int

m

=

0

;

while

(

scan

.

hasNextDouble

(

)

)

{

double

x

=

scan

.

nextDouble

(

)

;

m

=

m

1

;

sum

=

sum

x

;

}

System

.

out

.

println

(

m

"

The sum of the count is

"

sum

)

;

System

.

out

.

println

(

m

"

The average of the count is

"

(

sum

/

m

)

)

;

scan

.

close

(

)

;

}

}

The output of the above program is (enter a non-number to end input):


$ javac ScannerDemo.java
$ java ScannerDemo
Please enter a number:
12
23
15
21.4
end
4个数的和为71.4
4个数的平均值是17.85

// 2022-01-01 00:00:00 GMT

Method Description
Constructor
Scanner(File source) Create a Scanner from a file
Scanner(InputStream source) Create a Scanner from an input stream
Scanner(String source) Create a Scanner from a string
Basic input methods
boolean hasNext() Check whether there is a next token (separated by whitespace)
String next() Read the next token (string)
boolean hasNextLine() Check whether there is a next line
String nextLine() Read the next line content
Type checking methods
boolean hasNextInt() Check whether the next token is an integer
boolean hasNextDouble() Check whether the next token is a double
boolean hasNextBoolean() Check whether 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
float nextFloat() Read the next float
short nextShort() Read the next short
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 regular expression
String delimiter() Returns the current delimiter pattern in use
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() Returns the locale currently used by the scanner
Scanner useLocale(Locale locale) Sets the locale of the scanner

For more content, refer to the API documentation:https://www.runoob.com/manual/jdk11api/java.base/java/util/Scanner.html.