Home Java Scanner Methods nextByte() Java Scanner nextByte() Method Definition In Java, sc.nextByte() reads the next token and parses it into an 8-bit signed byte (ranging from -128 to 127). Like the other integer readers, it also accepts a radix from 2 to 36.
Because Java has no unsigned byte type, inputs like 128 or 255 throw InputMismatchException in base 10 unless handled with a wider type.
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.nextByte(...).
Syntax Read tiny signed integers or binary bit patterns using nextByte() or nextByte(radix).
Scanner sc = new Scanner("-12 127 0101");
byte temp = sc.nextByte(); // -12
byte peak = sc.nextByte(); // 127
byte bits = sc.nextByte(2); // 5 Call What it does sc.nextByte()Reads a signed byte (-128 to 127) using the current radix. sc.nextByte(2)Parses an 8-bit binary pattern up to 1111111 (127). sc.hasNextByte()Checks whether the token fits in a signed byte first.
nextByte() parses text representations like "100" into the numeric byte 100; it does not read raw binary bytes from a stream.
Library declarations // java.util.Scanner
public byte nextByte()
public byte nextByte(int radix) Parameters Parameter Description radix (optional)Number base (2 to 36) for parsing digits.
Return Value Return Value Description byteAn 8-bit signed integer between -128 and 127. Throws InputMismatchException If the token cannot parse or is outside -128..127. Throws NoSuchElementException If input has no more tokens. Throws IllegalStateException If the scanner is closed.
Compare with Similar Methods Comparing Scanner's 8-bit reader with wider types:
Method How it differs hasNextByte()Checks -128..127 range before reading. nextShort()Handles 16-bit values (-32768 to 32767). nextInt()Best choice when values reach 128..255. Byte.parseByte(s)Parses a String directly; throws NumberFormatException.
Java Examples 1. Signed byte bounds 2. 300 and 128 trap 3. Binary flags Example 1 — Read Signed Temperature Deltas Within -128..127 Parse signed 8-bit adjustments including the exact Byte.MIN_VALUE and Byte.MAX_VALUE limits.
import java.util.Scanner;
public class ScannerNextByteBounds {
public static void main(String[] args) {
try (Scanner sc = new Scanner("-128 -5 0 127")) {
while (sc.hasNextByte()) {
byte delta = sc.nextByte();
System.out.println("Delta: " + delta);
}
}
}
} Delta: -128
Delta: -5
Delta: 0
Delta: 127 How It Works Exact signed range. -128 is the lowest valid byte and 127 is the highest, so all four tokens succeed.Guarded loop. hasNextByte() verifies each token fits inside 8 bits before nextByte() reads it.Compact storage. Storing each reading in a byte keeps small signed numbers in a single 8-bit variable.Example 2 — Why 128 and 300 Fail nextByte() with InputMismatchException Demonstrate how values above 127 trigger InputMismatchException and how to skip or widen them.
import java.util.InputMismatchException;
import java.util.Scanner;
public class ScannerNextByteTrap {
public static void main(String[] args) {
try (Scanner sc = new Scanner("120 128 300")) {
while (sc.hasNext()) {
try {
System.out.println("Byte: " + sc.nextByte());
} catch (InputMismatchException e) {
System.out.println("Out of byte range (" + e.getClass().getSimpleName() + "): " + sc.nextInt());
}
}
}
}
} Byte: 120
Out of byte range (InputMismatchException): 128
Out of byte range (InputMismatchException): 300 How It Works 120 fits. Values up to 127 pass nextByte() without error.128 overflows by one. Because Java bytes are signed, 128 and 300 throw InputMismatchException.Widen on retry. Calling sc.nextInt() inside catch consumes and prints the unparsed token as an int.Example 3 — Parse Binary Flag Bytes with nextByte(2) Use radix 2 to convert binary bitstrings from text directly into byte values.
import java.util.Scanner;
public class ScannerNextByteBinary {
public static void main(String[] args) {
try (Scanner sc = new Scanner("00001010 01111111 -10000000")) {
byte a = sc.nextByte(2);
byte b = sc.nextByte(2);
byte c = sc.nextByte(2);
System.out.println("00001010 -> " + a);
System.out.println("01111111 -> " + b);
System.out.println("-10000000 -> " + c);
}
}
} 00001010 -> 10
01111111 -> 127
-10000000 -> -128 How It Works Radix 2 reads bits. Passing 2 interprets 00001010 as binary 10.Seven 1-bits equal 127. 01111111 is the largest positive binary value a signed byte accepts.Signed binary minimum. Prefixing - to 10000000 gives -128, matching Byte.MIN_VALUE.Frequently Asked Questions What is the valid range for Scanner.nextByte()? Signed 8-bit integers from -128 to 127 inclusive.
Why does Scanner.nextByte() fail on 200? In Java, byte is always signed with a maximum of 127. Use nextInt() for 0..255 numbers such as RGB channels.
Does nextByte() read raw binary bytes from a file? No. Scanner parses text tokens like "42" into a byte value. For raw binary files, use InputStream or Files.readAllBytes().
What exception is thrown when nextByte() receives "300"? InputMismatchException, and the token "300" remains unread in the scanner.
Can I pass a radix to nextByte()? Yes. Use sc.nextByte(2) for binary or sc.nextByte(16) for hex (up to 7f).
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
11 people found this page helpful
Helpful Share Copy link Suggestion