The Scanner class in Java belongs to the java.util package that takes the input from the user. Therefore it is widely used since it is simple and easy to implement. The initial step involves declaring the scanner object and then utilizing the methods available like nextBoolean(), nextDouble(), nextInt(), nextByte(), etc. of the scanner class according to the need of the program.
In this article, we will discuss the following topics in detail.
- What is the Scanner nextInt() Method?
- Implementation of the Scanner nextInt() Method without Radix
- Implementation of the Scanner nextInt() Method with Radix
- Exceptions While Working with the nextInt() Method
- Separate Integers From String Using nextInt() Method
What is the Scanner nextInt() Method?
The nextInt() method of the scanner class in Java reads, scans, or parses the next integer token in the input. This method is implemented with the hasNext() method which checks if the next input token is available or not. The Scanner nextInt() method is implemented in two different ways.
- The first way is without passing radix
- The second is with radix
Implementation of the nextInt() Method Without Radix
The code below shows the implementation of the nextInt() method of the Scanner class in Java.
//Import the required package
import java.util.Scanner;
//Declare the class in Java
public class ScannerExample {
public static void main(String[] args) {
//Declare the string
final String str = "10 12 15 17 19";
Scanner scan = new Scanner(str);
System.out.println("The Output is:");
//Print the output using the scanner methods
while (scan.hasNext()) {
System.out.println(scan.nextInt());
}
}
}
In the above code block:
- The package for the Scanner class is imported.
- In the main method of Java, the string str is declared using the final keyword.
- An object “scan” is created for the scanner class in Java.
- The while loop in the last step contains the hasNext() method that checks if there is a next token present in the input.
Output
The output below shows that all the entries declared in the input are printed as the result.

Scanner nextInt() Method to Read the Integer Input
The code below shows that the nextInt() method of the scanner class in Java reads the integer input.
//Import the Package
import java.util.Scanner;
//Declare the class
class IntegerExample {
public static void main(String[] args) {
//Create an object for the Scanner
Scanner i = new Scanner(System.in);
//Take input for the Integer
System.out.println("Enter any Integer: ");
// reads the declared integer value
int result = i.nextInt();
System.out.println("The value using the nextInt() is: " + result);
i.close();
}
}
In the above code block:
- The input from the user is taken using the Scanner class.
- The value of the declared integer is read using the nextInt() method of the scanner class in Java and printed to the screen.
Output
The output below shows that the value of the integer is printed using the nextInt() method.

Implementation of the nextInt(int i) Method With Radix
The nextInt() method when declared with the radix converts the input token to the integer value with the help of the radix. For instance, the radix declared as “4” will cater to 0, 1, 2, and 3 values. The Integer “4” will raise an exception that will be discussed later in this article.
//Import the package required
import java.util.Scanner;
public class ScannerExample {
//the main() method of Java
public static void main(String[] args) {
final String s = "1 3 7 6 5 4";
Scanner sc = new Scanner(s);
System.out.println("The Output is:");
while (sc.hasNext()) {
System.out.println(sc.nextInt(8));
}
}
}
In the above code block:
- The string is declared as “s” using the final keyword and is assigned with different values.
- An object for the scanner class is created as “sc” in Java.
- The while loop in the last part of the code utilizes the hasNext() method to check for the next input token and the nextInt() that is declared with the radix of value “8”.
Output
The values declared as the input are printed to the output. It is to keep in mind that in the case where the radix is explicitly defined in the nextInt() method, the result is printed up to the specified value of the radix otherwise it throws an exception.

Now, this article will advance to understand the exception raised while utilizing the nextInt() method and the practical implementations of the nextInt() method.
Exception While Working with the nextInt() Method
There are three different exceptions raised while implementing the nextInt() method. These exceptions are:
- InputMismatchException
- IllegalStateException
- NoSuchElementException
Let us discuss the exceptions one by one.
InputMismatchException
This exception arises while working with the radix in the nextInt() method of Java as depicted in the code below.
//Import the package required
import java.util.Scanner;
public class ScannerExample {
//the main() method of Java
public static void main(String[] args) {
final String s = "1 3 7 6 ";
Scanner sc = new Scanner(s);
System.out.println("The Output is:");
while (sc.hasNext()) {
System.out.println(sc.nextInt(5));
}
}
}
In the above code block:
- The values in the string are declared as “1 3 7 6 ”.
- The while loop contains the nextInt() method with the radix declared as “5” in the println() method.
Output
The output below shows that since the radix is declared as “5” and the values in the input are declared as “6 and 7” which exceeds the base 5, causing the exception on the value “7” without moving further.

IllegalStateException
This exception in Java is raised when an illegal statement is passed. The code example below shows that the close() method is declared before the hasNext() method which results in the exception.
//Import the package required
import java.util.Scanner;
public class ScannerExample {
//the main() method of Java
public static void main(String[] args) {
final String s = "1 3 7 6 ";
Scanner sc = new Scanner(s);
System.out.println("The Output is:");
sc.close();
while (sc.hasNext()) {
System.out.println(sc.nextInt(5));
}
}
}
Output
This shows that the IllegalStateException is raised because of the close() method.

NoSuchElementException
The code below shows how “NoSuchElementException” is raised while working with the nextInt() method in Java.
//Import the package required
import java.util.Scanner;
public class ScannerExample {
//the main() method of Java
public static void main(String[] args) {
final String s = "1 3 7 6 ";
Scanner sc = new Scanner(s);
while (true) {
System.out.println(sc.nextInt());
}
}
}
In the above code block:
- The while loop contains “true” and the nextInt() method that moves through the next integer token.
Output
The output below shows that the values of the string are printed and the “NoSuchElementException” is raised when the while loop doesn’t find the next value in the string.

Separate Integers From String Using nextInt() Method
The nextInt() method of the scanner class can separate the integers from the declared strings as depicted in the code below.
//Import the scanner package
import java.util.Scanner;
//Declare the class
public class ScannerExample {
public static void SpotInt(String input) {
System.out.println(input);
Scanner sc = new Scanner(input);
while (sc.hasNext()) {
if (sc.hasNextInt()) {
// calling the nextInt() method
System.out.println(sc.nextInt() +" Integer Spotted");
} else {
System.out.println(sc.next());
}
}
sc.close();
System.out.println();
}
public static void main(String[] args) {
String data1 = "I Secured 89 in Maths and 80 in English";
String data2 = "3 hi 5 11 @";
SpotInt(data1);
SpotInt(data2);
}
}
In the above code block:
- The required package is imported.
- The class “SannerExample” is declared in Java.
- The while loop contains the hasNext() method which checks for the next token.
- In the next step, the if-else statement checks whether the integer is present.
- In the main() method of Java, there are two strings declared as “data1” and “data2”.
- The integers are then returned using the method “SpotInt” in the last step.
Output
The output below shows that the integers from the strings are spotted using the nextInt() method of the scanner class in Java.

This concludes the implementation of the nextInt() method of the scanner class in Java.
Conclusion
The nextInt() method of the Java scanner class reads the integer values from the input provided. This method is also used to spot the integer among the strings in Java. In this article, we have implemented the nextInt() method of the scanner class to show how it works in Java and how exceptions are raised while working with this method.