java.util.Scanner nextShort()

Description :

This java tutorial shows how to use the nextShort method of Scanner class of java.util package. This method returns the short data type which corresponds to the short token on the scanner buffer.

Method Syntax :

There are two overloaded method of Scanner nextShort () java method

java scanner nextShort overload method
Overloaded methods are those method with same name but with different method signatures

Method 1:

public byte nextShort ()

Method 2:

public byte nextShort(int radix)

Parameter Input :

int|radix|the base number to scan

Method Returns :

This method simply returns the tokens of short data type.

Compatibility Version :

Requires Java 1.5 and up

Exception :


– if the next token does not match the Integer regular expression, or is out of range (more than the maximum value or less than the minimum value.


– if input is exhausted and we still invoked the nextShort() method.


– if the java scanner is closed and we still invoked the nextShort() method.

Discussion :

The Scanner nextShort() method simply returns short if the token on the scanner buffer can be interpreted or translated of short data type. There are two overloaded method of nextShort method, which accomplishes the same task. The method without a method argument is the same as calling nextShort(radix) which is the default radix of the Scanner object. The radix currently set by the Scanner object is defined using the method useRadix(radix) and can be determine using the radix() method. The radix integer on the method argument nextShort(radix) will override the default radix already defined to the Scanner object.

Make a note that since we are scanning tokens in short data type, there is limit on the maximum and minimum value possible. The Short.MAX_VALUE and Short.MIN_VALUE will show what is the maximum and minimum possible short value than can be evaluated.

Java Code Example :

This java example source code demonstrates the use of nextShort method of Scanner class. Basically the code just prints those tokens of short data type on the input string declared on the Scanner constructor. The radix used on the example below is 8, means to say we have scanned data type in octal base number and then prints the equivalent in decimal format.

Sample Output :

Running the nextShort() method example source code of Scanner class will give you the following output

java scanner nextShort method example

java scanner nextShort method example

Exception Scenario :

Similar Method :

  • N/A

Suggested Reading List :

References :