In Java, sc.hasNextShort() returns true if the next token parses as a signed 16-bit short between -32768 and 32767, without consuming any input.
Pass an optional radix (2 to 36) with hasNextShort(int radix) to validate binary, octal, or hexadecimal 16-bit values.
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.hasNextShort(...).
Syntax
Verify 16-bit bounds safely before calling nextShort().
Java
Scanner sc = new Scanner("32767 32768");
boolean ok = sc.hasNextShort(); // true
sc.next();
boolean tooBig = sc.hasNextShort(); // false
Call
What it does
sc.hasNextShort()
Checks signed 16-bit range in current radix.
sc.hasNextShort(16)
Checks signed 16-bit range in hexadecimal (up to 7fff).
Even though "40000" is a valid integer, hasNextShort() returns false because 40000 exceeds Short.MAX_VALUE (32767).
Library declarations
Java
// java.util.Scanner
public boolean hasNextShort()
public boolean hasNextShort(int radix)
Parameters
Parameter
Description
radix (optional)
Number base (2 to 36) for parsing digits.
Return Value
Return Value
Description
boolean
true if the next token fits in -32768..32767; false otherwise.
Exact endpoints pass.-32768 and 32767 are the exact signed 16-bit edges and return true.
Off-by-one edges fail safely.-32769 and 32768 return false without crashing.
Single-pass consumption. Printing sc.next() after testing advances cleanly to the next candidate.
Example 2 — Check Signed 16-Bit Hex Tokens with hasNextShort(16)
Test why 7fff and -8000 pass base-16 short validation while 8000 fails.
Java
import java.util.Scanner;
public class ScannerHasNextShortHex {
public static void main(String[] args) {
try (Scanner sc = new Scanner("7fff 8000 -8000")) {
while (sc.hasNext()) {
if (sc.hasNextShort(16)) {
System.out.println("Valid short hex -> " + sc.nextShort(16));
} else {
System.out.println("Too large for signed short: " + sc.next());
}
}
}
}
}
Output
Valid short hex -> 32767
Too large for signed short: 8000
Valid short hex -> -32768
How It Works
Positive hex ceiling.7fff equals 32767 and passes hasNextShort(16).
Unsigned 8000 overflows. Positive hex 8000 equals 32768, returning false for signed short.
Negative hex floor fits. Adding the minus sign to -8000 yields -32768, matching Short.MIN_VALUE.
Example 3 — Extract Valid Short Values from Mixed Status Logs
Keep only tokens within signed 16-bit range from a mixed telemetry line.
Java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ScannerHasNextShortFilter {
public static void main(String[] args) {
String log = "200 ok 32000 50000 -10";
List<Short> kept = new ArrayList<>();
try (Scanner sc = new Scanner(log)) {
while (sc.hasNext()) {
if (sc.hasNextShort()) {
kept.add(sc.nextShort());
} else {
sc.next();
}
}
}
System.out.println("Kept shorts: " + kept);
}
}
Output
Kept shorts: [200, 32000, -10]
How It Works
Skip text and oversized numbers alike. Both "ok" and 50000 return false and are discarded by sc.next().
Boxed into List<Short>. Valid tokens 200, 32000, and -10 are autoboxed into the list.
Clean filtered list. All three in-range shorts appear in input order.
Frequently Asked Questions
Signed 16-bit integers from -32768 to 32767 inclusive.
Java short is signed with a maximum of 32767, so 65535 is out of range.
No. Any decimal point causes hasNextShort() to return false.