java.util.Scanner hasNextShort()

Description :

This java tutorial shows how to use the hasNextShort method of Scanner class of java.util package. This method returns boolean, true if  the token can be interpreted as short data type with respect to the radix used by the scanner object otherwise false.

Method Syntax :

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

java scanner hasNextShort overload method
Note:
Overloaded methods are those method with same name but with different method signatures


Method 1:

public byte hasNextShort()

Method 2:

public byte hasNextShort(int radix)

Parameter Input :

[table]
DataType|Parameter|Description
int|radix|the base number to scan
[/table]

Method Returns :

This method simply returns true if the scanner token is of short data type.

Compatibility Version :

Requires Java 1.5 and up

Exception :

IllegalStateException

– if this scanner is closed and we still invoked the hasNextShort method

Discussion :

The Scanner hasNextShort() method simply return true if the token on the scanner buffer can be interpreter or translated of short data type. There are two overloaded method of hasNextShort method, which accomplishes the same task. The method without a method argument is the same as calling hasNextShort(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. Calling hasNextShort(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 hasNextShort 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 radix 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 hasNextShort() method example source code of Scanner class will give you the following output

java scanner hasNextShort method example

java scanner hasNextShort method example

Exception Scenario :

Similar Method :

  • N/A

Suggested Reading List :

References :