In Java, sc.hasNextByte() returns true only when the next token represents an integer within signed 8-bit bounds (-128 to 127), without advancing the scanner.
Passing a radix (2 to 36) lets you test binary, octal, or hex text before reading with nextByte().
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.hasNextByte(...).
Syntax
Check 8-bit signed bounds or binary bitstrings before consuming them.
Java
Scanner sc = new Scanner("-128 127 128");
boolean ok = sc.hasNextByte(); // true
Call
What it does
sc.hasNextByte()
Tests if the token fits in -128..127 (current radix).
sc.hasNextByte(2)
Tests an 8-bit signed binary token.
Because signed byte tops out at 127, tokens like 128 or 255 return false.
Library declarations
Java
// java.util.Scanner
public boolean hasNextByte()
public boolean hasNextByte(int radix)
Parameters
Parameter
Description
radix (optional)
Number base (2 to 36) used to inspect digits.
Return Value
Return Value
Description
boolean
true if a token exists and fits in -128..127; false otherwise.