Java Scanner hasNextByte() Method

The hasNextByte() is a Java Scanner class method which is used to check the next token in this scanner's input can be interpreted as a byte value in the default radix or not. It returns true if it is interprated as a byte value, otherwise returns false. There is two different types of Java hasNextByte() method which can be differentiated depending on its parameter. These are:

  1. Java Scanner hasNextByte() Method
  2. Java Scanner hasNextByte(int radix) Method

hasNextByte() Method:

This is a Java Scanner class method which is used to check the next token in this scanner's input can be interpreted as a byte value in the default radix using the nextByte() method.

hasNextByte(int radix) Method:

This is an inbuilt method of Java Scanner class which is used to check the next token in this scanner's input can be interpreted as a byte value in the specified radix using the nextByte() method.

Syntax

Following is the declaration of hasNextByte() method:

Parameter

DataTypeParameterDescriptionRequired/Optional
intradixIt is used to interpret the token as a byte value.Required

Returns

The hasNextByte() method returns true if and only if this scanner's next token is a valid byte value.

Exceptions

IllegalStateException- This method throws exception if the innvocation is done after the scanner has been closed.

IllegalArgumentException- It throws this exception if the specified radix is out of range.

Compatibility Version

Java 1.5 and above

Example 1

Output:

Scan String: JavaTpoint.com
Result: true
Scan String: 11
Result: false
Scan String: +
Result: true
Scan String: 11
Result: false
Scan String: =
Result: false
Scan String: 22.0
Result: false

Example 2

Output:

Found a byte!

Example 3

Output:

Not Found :JavaTpoint.com
Found :7
Not Found :+
Found :7
Not Found :=
Not Found :22.0

Example 4

Output:

Exception in thread "main" java.lang.IllegalArgumentException: radix:598670
	at java.base/java.util.Scanner.setRadix(Scanner.java:1368)
	at java.base/java.util.Scanner.nextByte(Scanner.java:1998)
	at myPackage.ScannerHasNextByteExample4.main(ScannerHasNextByteExample4.java:10)