Java Scanner nextInt() MethodThe 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:
nextInt() MethodThis Java Scanner class method is used to scan the next token of the input as an int. nextInt(int radix) MethodThis 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. SyntaxFollowing is the declaration of nextInt() method: Parameter
ReturnsThe nextInt() method returns the int value scanned from the input. ExceptionsInputMismatchException- 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 VersionJava 1.5 and above Example 1Output: 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 2Output: Enter the amount: 800 Enter the Total Balance: 1500 New Balance is: 200 Example 3Output: Number: 12345 String: JavaTpoint Output: 12345, JavaTpoint Example 4Output: 85 179 -37 -7 29 Example 5Output: 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
|