Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Scanner nextByte() method in Java with Examples
Next article icon

Scanner nextByte() method in Java with Examples

Last Updated : 12 Oct, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
The nextByte(radix) method of java.util.Scanner class scans the next token of the input as a Byte. If the translation is successful, the scanner advances past the input that matched. If the parameter radix is not passed, then it behaves similarly as nextByte(radix) where the radix is assumed to be the default radix. Syntax:
public byte nextByte()
Parameters: The function accepts a parameter radix which is used to Byteerpret the token as a Byte value. Return Value: This function returns the byte scanned from the input. Exceptions: The function throws three exceptions as described below:
  • InputMismatchException: if the next token does not matches the Byteeger regular expression, or is out of range
  • NoSuchElementException: throws if input is exhausted
  • IllegalStateException: throws if this scanner is closed
Below programs illustrate the above function: Program 1: Java
// Java program to illustrate the // nextByte() method of Scanner class in Java // without parameter  import java.util.*;  public class GFG1 {     public static void main(String[] argv)         throws Exception     {          String s = "Gfg 9 + 6 = 12.0";          // create a new scanner         // with the specified String Object         Scanner scanner = new Scanner(s);          while (scanner.hasNext()) {              // if the next is a Byte,             // prByte found and the Byte             if (scanner.hasNextByte()) {                 System.out.println("Found Byte value :"                                    + scanner.nextByte());             }              // if no Byte is found,             // prByte "Not Found:" and the token             else {                 System.out.println("Not found Byte value :"                                    + scanner.next());             }         }         scanner.close();     } } 
Output:
  Not found Byte value :Gfg  Found Byte value :9  Not found Byte value :+  Found Byte value :6  Not found Byte value :=  Not found Byte value :12.0  
Program 2: Java
// Java program to illustrate the // nextByte() method of Scanner class in Java // with parameter  import java.util.*;  public class GFG1 {     public static void main(String[] argv)         throws Exception     {          String s = "Gfg 9 + 6 = 12.0";          // create a new scanner         // with the specified String Object         Scanner scanner = new Scanner(s);          while (scanner.hasNext()) {              // if the next is a Byte,             // prByte found and the Byte             if (scanner.hasNextByte()) {                 System.out.println("Found Byte value :"                                    + scanner.nextByte(12));             }              // if no Byte is found,             // prByte "Not Found:" and the token             else {                 System.out.println("Not found Byte value :"                                    + scanner.next());             }         }         scanner.close();     } } 
Output:
  Not found Byte value :Gfg  Found Byte value :9  Not found Byte value :+  Found Byte value :6  Not found Byte value :=  Not found Byte value :12.0  
Program 3: To demonstrate InputMismatchException Java
// Java program to illustrate the // nextByte() method of Scanner class in Java // InputMismatchException  import java.util.*;  public class GFG1 {     public static void main(String[] argv)         throws Exception     {          try {              String s = "Gfg 9 + 6 = 12.0";              // create a new scanner             // with the specified String Object             Scanner scanner = new Scanner(s);              while (scanner.hasNext()) {                  // if the next is a Byte,                 // prByte found and the Byte                 // since the value 60 is out of range                 // it throws an exception                  System.out.println("Next Byte value :"                                    + scanner.nextByte());             }             scanner.close();         }         catch (Exception e) {             System.out.println("Exception thrown: " + e);         }     } } 
Output:
  Exception thrown: java.util.InputMismatchException  
Program 4: To demonstrate NoSuchElementException Java
// Java program to illustrate the // nextByte() method of Scanner class in Java // NoSuchElementException  import java.util.*;  public class GFG1 {     public static void main(String[] argv)         throws Exception     {          try {              String s = "Gfg";              // create a new scanner             // with the specified String Object             Scanner scanner = new Scanner(s);              // Trying to get the next Byte value             // more times than the scanner             // Hence it will throw exception             for (int i = 0; i < 5; i++) {                  // if the next is a Byte,                 // prByte found and the Byte                 if (scanner.hasNextByte()) {                     System.out.println("Found Byte value :"                                        + scanner.nextByte());                 }                  // if no Byte is found,                 // prByte "Not Found:" and the token                 else {                     System.out.println("Not found Byte value :"                                        + scanner.next());                 }             }             scanner.close();         }         catch (Exception e) {             System.out.println("Exception thrown: " + e);         }     } } 
Output:
  Not found Byte value :Gfg  Exception thrown: java.util.NoSuchElementException  
Program 5: To demonstrate IllegalStateException Java
// Java program to illustrate the // nextByte() method of Scanner class in Java // IllegalStateException  import java.util.*;  public class GFG1 {     public static void main(String[] argv)         throws Exception     {          try {              String s = "Gfg 9 + 6 = 12.0";              // create a new scanner             // with the specified String Object             Scanner scanner = new Scanner(s);              // close the scanner             scanner.close();             System.out.println("Scanner Closed");              System.out.println("Trying to get "                                + "next Byte value");              while (scanner.hasNext()) {                  // if the next is a Byte,                 // prByte found and the Byte                 if (scanner.hasNextByte()) {                     System.out.println("Found Byte value :"                                        + scanner.nextByte());                 }                  // if no Byte is found,                 // prByte "Not Found:" and the token                 else {                     System.out.println("Not found Byte value :"                                        + scanner.next());                 }             }         }         catch (Exception e) {             System.out.println("Exception thrown: " + e);         }     } } 
Output:
  Scanner Closed  Trying to get next Byte value  Exception thrown: java.lang.IllegalStateException: Scanner closed  
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextByte()

Next Article
Scanner nextByte() method in Java with Examples

G

gopaldave
Improve
Article Tags :
  • Java
  • Java-Library
  • Java - util package
  • Java-Functions
Practice Tags :
  • Java

Similar Reads

    Scanner nextInt() method in Java with Examples
    The nextInt(radix) method of java.util.Scanner class scans the next token of the input as a Int. If the translation is successful, the scanner advances past the input that matched. If the parameter radix is not passed, then it behaves similarly as nextInt(radix) where the radix is assumed to be the
    5 min read
    Scanner nextLine() method in Java with Examples
    The nextLine() method of java.util.Scanner class advances this scanner past the current line and returns the input that was skipped. This function prints the rest of the current line, leaving out the line separator at the end. The next is set to after the line separator. Since this method continues
    2 min read
    Scanner nextDouble() method in Java with Examples
    The nextDouble() method of java.util.Scanner class scans the next token of the input as a Double. If the translation is successful, the scanner advances past the input that matched. Syntax: public double nextDouble() Parameters: The function does not accepts any parameter. Return Value: This functio
    4 min read
    Scanner nextBigInteger() method in Java with Examples
    The nextBigInteger(radix) method of java.util.Scanner class scans the next token of the input as a BigInteger. If the translation is successful, the scanner advances past the input that matched. If the parameter radix is not passed, then it behaves similarly as nextBigInteger(radix) where the radix
    5 min read
    Scanner nextShort() method in Java with Examples
    The nextShort(radix) method of java.util.Scanner class scans the next token of the input as a short. If the translation is successful, the scanner advances past the input that matched. If the parameter radix is not passed, then it behaves similarly as nextShort(radix) where the radix is assumed to b
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences