Home » Java programming language

Java Scanner nextByte() Method with Example

Scanner Class nextByte() method: Here, we are going to learn about the nextByte() method of Scanner Class with its syntax and example.
Submitted by Preeti Jain, on March 26, 2020

Scanner Class nextByte() method


    public byte nextByte();
    public byte nextByte(int rad);
  • nextByte() method is available in java.util package.
  • nextByte() method is used to read the next token of the input as a byte value at the implicit radix (rad) of this Scanner object.
  • nextByte(int rad) method is used to read the next token of the input as a byte value at the explicit or given radix (rad) of this Scanner object.
  • These methods may throw an exception at the time of representing input as a byte.
    • InputMismatchException: This exception may throw when the next token of the input mismatch.
    • NoSuchElementException: This exception may throw when no such element exists
    • IllegalStateException: This exception may throw when this Scanner is not opened.
  • These are non-static methods, it is accessible with class object & if we try to access these methods with the class name then we will get an error.


  • In the first case, nextByte(),
    • It does not accept any parameter.
  • In the second case, nextByte(int rad),
    • int rad – represents the radix used to manipulate the token as a byte.

Return value:

In both the cases, the return type of the method is byte, it retrieves the byte read from the input.


// Java program is to demonstrate the example
// of nextByte() of Scanner

import java.util.*;
import java.util.regex.*;

public class NextByte {
 public static void main(String[] args) {
  String str = "Java Programming! 3 * 8= 24 + b";
  byte b = 10;

  // Instantiates Scanner
  Scanner sc = new Scanner(str);

  while (sc.hasNext()) {
   // By using nextByte() method isto
   // return the next token as a 
   // byte in the default radix
   if (sc.hasNextByte()) {
    byte next_b = sc.nextByte();
    System.out.println("sc.nextByte()): " + next_b);

   // By using nextByte(radix) is to return
   // the byte value with the given radix
   if (sc.hasNextByte()) {
    byte next_r = sc.nextByte(9);
    System.out.println("sc.nextByte(9)): " + next_r);
  // Scanner closed


sc.nextByte()): 3
sc.nextByte()): 24


Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.