Java Scanner nextByte() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.util.Scanner

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).

Java
Scanner sc = new Scanner("-12 127 0101");
byte temp = sc.nextByte();       // -12
byte peak = sc.nextByte();       // 127
byte bits = sc.nextByte(2);      // 5
CallWhat 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
// java.util.Scanner
public byte nextByte()
public byte nextByte(int radix)

Parameters

ParameterDescription
radix (optional)Number base (2 to 36) for parsing digits.

Return Value

Return ValueDescription
byteAn 8-bit signed integer between -128 and 127.
Throws InputMismatchExceptionIf the token cannot parse or is outside -128..127.
Throws NoSuchElementExceptionIf input has no more tokens.
Throws IllegalStateExceptionIf the scanner is closed.

Compare with Similar Methods

Comparing Scanner's 8-bit reader with wider types:

MethodHow 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

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.

Java
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);
            }
        }
    }
}

How It Works

  1. Exact signed range. -128 is the lowest valid byte and 127 is the highest, so all four tokens succeed.
  2. Guarded loop. hasNextByte() verifies each token fits inside 8 bits before nextByte() reads it.
  3. 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.

Java
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());
                }
            }
        }
    }
}

How It Works

  1. 120 fits. Values up to 127 pass nextByte() without error.
  2. 128 overflows by one. Because Java bytes are signed, 128 and 300 throw InputMismatchException.
  3. 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.

Java
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);
        }
    }
}

How It Works

  1. Radix 2 reads bits. Passing 2 interprets 00001010 as binary 10.
  2. Seven 1-bits equal 127. 01111111 is the largest positive binary value a signed byte accepts.
  3. Signed binary minimum. Prefixing - to 10000000 gives -128, matching Byte.MIN_VALUE.

Frequently Asked Questions

Signed 8-bit integers from -128 to 127 inclusive.
In Java, byte is always signed with a maximum of 127. Use nextInt() for 0..255 numbers such as RGB channels.
No. Scanner parses text tokens like "42" into a byte value. For raw binary files, use InputStream or Files.readAllBytes().
InputMismatchException, and the token "300" remains unread in the scanner.
Yes. Use sc.nextByte(2) for binary or sc.nextByte(16) for hex (up to 7f).

Next: nextDouble()

Read the next token as a double.

nextDouble() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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