Javatpoint Logo
Javatpoint Logo

Java Scanner nextInt() Method

The nextInt() method of Java Scanner class is used to scan the next token of the input as an int. There is two different types of Java nextInt() method which can be differentiated depending on its parameter. These are:

  • Java Scanner nextInt() Method
  • Java Scanner nextInt(int radix) Method

nextInt() Method

This Java Scanner class method is used to scan the next token of the input as an int.

nextInt(int radix) Method

This is an inbuilt method of Java Scanner class which is used to scan the next token of the input as an int in the specified radix.

Syntax

Following is the declaration of nextInt() method:

Parameter

DataType Parameter Description Required/Optional
int radix It is used to interpret the token as a int value. Required

Returns

The nextInt() method returns the int value scanned from the input.

Exceptions

InputMismatchException- This method throws exception if the next token does not match the Integer regular expression, or is out of range

IllegalStateException- It throws this exception if the innvocation is done after the scanner has been closed.

IllegalArgumentException- It throws this exception if the specified radix is out of range.

Compatibility Version

Java 1.5 and above

Example 1

Output:

Not Found Int value: Facebook.com
Found Int Value: 13
Not Found Int value: +
Not Found Int value: 3.0
Not Found Int value: =
Found Int Value: 16
Not Found Int value: true

Example 2

Output:

Enter the amount: 800
Enter the Total Balance: 1500
New Balance is: 200

Example 3

Output:

Number: 12345
String: JavaTpoint
Output: 12345, JavaTpoint

Example 4

Output:

85
179
-37
-7
29

Example 5

Output:

Exception in thread "main" java.lang.IllegalArgumentException: radix:598670
	at java.base/java.util.Scanner.setRadix(Scanner.java:1368)
	at java.base/java.util.Scanner.nextLong(Scanner.java:2370)
	at myPackage.ScannerNextIntExample5.main(ScannerNextIntExample5.java:10)
Next TopicJava Scanner Class


Help Others, Please Share

facebook twitter pinterest