Javatpoint Logo
Javatpoint Logo

Java Scanner nextShort() Method

The nextShort() is a Java Scanner class method which is used to scan the next token of the input as a Short. There is two different types of Java nextShort() method which can be differentiated depending on its parameter. These are:

  • Java Scanner nextShort() Method
  • Java Scanner nextShort(int radix) Method

nextShort() Method

This is a Java Scanner class method which is used to scan the next token of the input as a Short in the default radix.

nextShort(int radix) Method

This is an inbuilt method of Java Scanner class which is used to scan the next token of the input as a Short in the specified radix.

Syntax

Following is the declaration of nextShort() method:

Parameter

DataType Parameter Description Required/Optional
int radix It is used to interpret the token as a short value. Required

Returns

The nextShort() method returns the Short value scanned from the input.

Exceptions

IllegalStateException- This method throws exception if the innvocation is done after the scanner has been closed.

IllegalArgumentException- It throws this exception if the specified radix is out of range.

InputMismatchException- It throws this exception if the next token does not match the Integer regular expression, or is out of range.

Compatibility Version

Java 1.5 and above

Example 1

Output:

Enter the Number you want: 40
Output value: 840

Example 2

Output:

Not Found :Facebook.com
Found :7
Not Found :+
Found :7
Not Found :=
Not Found :26.0
Not Found :true

Example 3

Output:

Not Found: JavaTpoint.com
Found: 13
Not Found: +
Found: 13
Not Found: =
Not Found: 26.0
Not Found: true
Found: 12345

Example 4

Output:

Exception in thread "main" java.lang.IllegalArgumentException: radix:555
	at java.base/java.util.Scanner.setRadix(Scanner.java:1368)
	at java.base/java.util.Scanner.nextShort(Scanner.java:2114)
	at myPackage.ScannerNextShortExample4.main(ScannerNextShortExample4.java:9)
Next TopicJava Scanner Class


Help Others, Please Share

facebook twitter pinterest