Java Scanner nextByte() Method

6 Nov 2024 | 3 min read

The nextByte() is a Java Scanner class method which is used to scan the next token of the input as a Byte. There is two different types of Java nextByte() method which can be differentiated depending on its parameter. These are:

  • Java Scanner nextByte() Method
  • Java Scanner nextByte(int radix) Method

nextByte() Method

This is a Java Scanner class method which is used to scan the next token of the input as a Byte in the default radix.

nextByte(int radix) Method

This is an inbuilt method of Java Scanner class which is used to scan the next token of the input as a Byte in the specified radix.

Syntax

Following is the declaration of nextByte() method:

Parameter

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

Returns

The nextByte() method returns the Byte value scanned from the input.

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.

InputMismatchException- It throws this exception if the next token does not match the Integer regular expression, or is out of range.

Compatibility Version

Java 1.5 and above

Example 1

Output:

Enter the Number you want: 20 Output value: 220 

Example 2

Output:

Enter Value of a: 55 Enter Value of x: 66 Enter Value of b: 88 Output: 88 

Example 3

Output:

Not Found :JavaTpoint.com Found :13 Not Found :+ Found :13 Not Found := Not Found :26.0 Not Found :false 

Example 4

Output:

85 53 -85 117 81 -122 

Example 5

Output:

Not Found : 22 Not Found : Java Exception in thread "main" java.lang.IllegalArgumentException: radix:2050	at java.base/java.util.Scanner.setRadix(Scanner.java:1368)	at java.base/java.util.Scanner.nextByte(Scanner.java:1998)	at myPackage.ScannerNextByteExample5.main(ScannerNextByteExample5.java:10)