In Java, sc.nextShort() reads the next token and parses it into a 16-bit signed short, covering values from -32768 to 32767. An optional radix parameter lets you read binary, octal, or hexadecimal values.
Any valid integer outside -32768..32767 (such as 40000) causes InputMismatchException rather than truncating bits.
An instance method of java.util.Scanner, available since Java 5 (JDK 1.5). A Scanner reads from any source (System.in for keyboard, a String, or a file) with identical parsing logic; pass the source to new Scanner(...) after import java.util.Scanner; and call sc.nextShort(...).
Syntax
Read 16-bit signed numbers directly with nextShort() or nextShort(radix).
Java
Scanner sc = new Scanner("2026 -15 7fff");
short year = sc.nextShort(); // 2026
short offset = sc.nextShort(); // -15
short hexMax = sc.nextShort(16); // 32767
Call
What it does
sc.nextShort()
Reads a short in the scanner's current radix.
sc.nextShort(16)
Parses a 16-bit hex value up to 7fff (32767).
sc.hasNextShort()
Checks whether the next token fits inside short bounds.
Even though Java's Short.parseShort throws NumberFormatException internally, Scanner catches it and throws InputMismatchException consistently for all bad or out-of-range tokens.
Library declarations
Java
// java.util.Scanner
public short nextShort()
public short nextShort(int radix)
Parameters
Parameter
Description
radix (optional)
The base (2 to 36) used to parse the digits.
Return Value
Return Value
Description
short
A 16-bit signed integer between -32768 and 32767.
Throws InputMismatchException
If the token is non-numeric or outside Short.MIN_VALUE..Short.MAX_VALUE.