Java Scanner hasNextByte() Method

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

Definition

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
CallWhat 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

ParameterDescription
radix (optional)Number base (2 to 36) used to inspect digits.

Return Value

Return ValueDescription
booleantrue if a token exists and fits in -128..127; false otherwise.
Throws IllegalStateExceptionIf the scanner has been closed.
Throws IllegalArgumentExceptionIf radix is below 2 or above 36.

Compare with Similar Methods

8-bit lookahead vs wider checks:

MethodHow it differs
nextByte()Consumes the verified signed byte.
hasNextShort()Accepts 16-bit values up to 32767.
hasNextInt()Needed for 0..255 unsigned byte inputs.

Java Examples

Example 1 — Check Values Around -128 and 127

Test five values spanning both boundaries of Java's signed byte range.

Java
import java.util.Scanner;

public class ScannerHasNextByteLimits {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner("-129 -128 0 127 128")) {
            while (sc.hasNext()) {
                boolean valid = sc.hasNextByte();
                System.out.println(sc.next() + " -> " + valid);
            }
        }
    }
}

How It Works

  1. Minimum boundary at -128. -129 is too negative and yields false, while -128 returns true.
  2. Maximum boundary at 127. 0 and 127 pass, while 128 returns false.
  3. Advance manually after check. Because hasNextByte() never consumes, sc.next() moves across each test value.

Example 2 — Validate Binary Tokens with hasNextByte(2)

Verify which binary strings fit inside a signed byte using radix 2.

Java
import java.util.Scanner;

public class ScannerHasNextByteBinary {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner("1111111 10000000 -10000000")) {
            while (sc.hasNext()) {
                if (sc.hasNextByte(2)) {
                    System.out.println("Fits byte: " + sc.nextByte(2));
                } else {
                    System.out.println("Too big in base 2: " + sc.next());
                }
            }
        }
    }
}

How It Works

  1. Seven 1s equal 127. 1111111 converts to 127 and passes hasNextByte(2).
  2. Unsigned 128 fails. 10000000 without a minus sign represents positive 128, so base-2 validation returns false.
  3. Signed -128 passes. Prefixing - puts -10000000 inside Byte.MIN_VALUE.

Example 3 — Collect Valid Signed Byte Offsets from Mixed Data

Separate small signed offsets from larger or non-numeric entries in one pass.

Java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ScannerHasNextByteFilter {
    public static void main(String[] args) {
        String data = "12 200 -45 bad 99";
        List<Byte> valid = new ArrayList<>();
        try (Scanner sc = new Scanner(data)) {
            while (sc.hasNext()) {
                if (sc.hasNextByte()) {
                    valid.add(sc.nextByte());
                } else {
                    sc.next();
                }
            }
        }
        System.out.println("Valid bytes: " + valid);
    }
}

How It Works

  1. Out-of-range skipped cleanly. 200 exceeds 127 and "bad" is non-numeric, so both hit the else branch.
  2. Valid offsets collected. 12, -45, and 99 pass hasNextByte() and are stored.
  3. No exceptions thrown. Guarding every nextByte() keeps the scan smooth across invalid entries.

Frequently Asked Questions

Whole numbers from -128 to 127 inclusive (in base 10, or in the given radix).
Java byte is signed and capped at 127, so 255 is out of range.
No. Decimal points make hasNextByte() return false.
Yes, pass 2..36 to hasNextByte(int radix), such as hasNextByte(2) or hasNextByte(16).
It returns false without throwing.

Next: hasNextDouble()

Check whether the next token can be parsed as a double.

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

15 people found this page helpful