Java

Scanner nextInt() Method in Java

The “nextInt()” method is utilized for scanning and parsing the next token of the input data in the form of “int”. It will scan the characters with the help of input steam. The input can be gathered and stored either in the form of a String, read from a particular file, real-time information/data, or any system input by the user.

This post will demonstrate the following:

What is the Scanner nextInt() Method in Java?

The “nextInt()” is a built-in method of a scanner object in Java that is used to read the characters one by one and modify them in an integer type. The Scanner object reads the digits one by one until it has gathered all integer values. Then, it will transform them into a 32-bit numeric value. Usually, that value is stored in an int variable.

How to Utilize the Scanner nextInt() Method in Java?

The syntax of the scanner nextInt() method in Java is mentioned below:

public int nextInt()

 

Example 1: Scanner nextInt() Method in While Loop

To utilize the “nextInt()” method inside the while loop, first of all, make a string with a particular name and pass the value to that string:

String s = "Linuxhint 12 - 5 = 7.0";

 

Create a new scanner with a particular string object using the “Scanner()” method:

Scanner abc = new Scanner(s);

 

In the below code snippet, we are looping through the whole “abc” scanner object using a while loop. Now, inside the while loop, if the next element is int, it will be printed as “Int value”; Otherwise, the next element will be printed as “Other value”:

while (abc.hasNext()) {
 if (abc.hasNextInt()) {
  System.out.println("Int value :" + abc.nextInt());
 }
 else {
 System.out.println("Other Value:" + abc.next());
 }
}

 

Call the “close()” function along with the scanner to display the result on the screen:

abc.close();

 

Output

In this example, using the “hasNextInt()” method, we are able to diagnose whether the next int is an integer or not and then print it accordingly. But, what if we replace this check and see what “nextInt()” method returns?

Let’s run the code again without the if/else block:

String s = "Linuxhint 12 - 5 = 7.0";
Scanner abc = new Scanner(s);
while (abc.hasNext()) {
  System.out.println("Int value :" + abc.nextInt());
}
abc.close();

 

As it can be seen in the output, it shows the “InputMismatchException”.

Example 2: Scanner nextInt() Method to handle InputMismatchException

To handle “inputMismatchException”, the try/catch statement can be used. Inside the “try” statement make a string with a particular name and create a scanner with a defined string object. Utilize the while iterator and pass the “has.Next()” method:

try {
 String s = "2+5+5 Linuxhint= 12.0";
 Scanner abc = new Scanner(s);
 while (abc.hasNext()) {
  System.out.println("Int Value:" + abc.nextInt());
}
 abc.close();
}

 

Invoke the “catch” statement to catch the error (if it exists) and print the output on the console:

catch (Exception e) {
 System.out.println("Exception: " + e);
}

 

As a result, you can see the “InputMismatchException” library on the console:

That’s all about the scanner nextInt() method in Java.

Conclusion

The “nextInt()” is a built-in method of a scanner object in Java that is utilized to read the characters one by one and modify them in an integer type. The specified Scanner object reads the digits sequentially until it gathers all integer values. This post has stated the nextInt() method in Java.

About the author

Hafsa Javed