Home » Java programming language

Java Scanner nextInt() Method with Example

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

Scanner Class nextInt() method

Syntax:

    public int nextInt();
    public int nextInt(int rad);
  • nextInt() method is available in java.util package.
  • nextInt() method is used to read the next token of the input as an int value at the implicit radix (rad) of this Scanner.
  • nextInt(int rad) method is used to read the next token of the input as an int value at the explicit or given radix (rad) of this Scanner.
  • These methods may throw an exception at the time of representing input as an int.
    • 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.

Parameter(s):

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

Return value:

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

Example 1:

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

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

public class NextInt {
 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 nextInt() method isto
   // return the next token as a 
   // int in the default radix
   if (sc.hasNextInt()) {
    int next_i = sc.nextInt();
    System.out.println("sc.nextInt()): " + next_i);
   }
   System.out.println(sc.next());
  }
  // Scanner closed
  sc.close();
 }
}

Output

Java
Programming!
sc.nextInt()): 3
*
8=
sc.nextInt()): 24
+
b

Example 2:

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

public class NextInt {
 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 nextInt(9) method isto
   // return the next token as a 
   // int in the given radix
   if (sc.hasNextInt()) {
    int next_i = sc.nextInt(9);
    System.out.println("sc.nextInt(9)): " + next_i);
   }
   System.out.println(sc.next());
  }
  // Scanner closed
  sc.close();
 }
}

Output

Java
Programming!
sc.nextInt(9)): 3
*
8=
sc.nextInt(9)): 22
+
b






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





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


© https://www.includehelp.com some rights reserved.