java.util.Scanner nextInt()
Description :
This java tutorial shows how to use the nextInt method of Scanner class of java.util package. This method returns the int data type which corresponds to the integer token on the scanner buffer.
Method Syntax :
There are two overloaded method of Scanner nexInt() java method
Note: Overloaded methods are those method with same name but with different method signatures
Method 1:
public int nextInt()
Method 2:
public int nextInt(int radix)
Parameter Input :
DataType | Parameter | Description |
---|---|---|
int | radix | the base number to scan |
Method Returns :
This method simply returns the tokens of int data type.
Compatibility Version :
Requires Java 1.5 and up
Exception :
InputMismatchException
if the next token does not match the Integer regular expression, or is out of range (more than the maximum value or less than the minimum value.
NoSuchElementException
if input is exhausted and we still invoked the nextInt() method.
IllegalStateException
if the java scanner is closed and we still invoked the nextInt() method.
Discussion :
The Scanner nextInt() method simply returns int if the token on the scanner buffer can be interpreted or translated into int data type. There are two overloaded method of nextInt method, which accomplishes the same task. The method without a method argument is the same as calling nextInt(radix) which is the default radix of the Scanner object. The radix currently set by the Scanner object is defined using the method useRadix(radix) and can be determine using the radix() method. The radix integer on the method argument nextInt(radix) will override the default radix already defined to the Scanner object.
Make a note that since we are scanning tokens in int data type, there is limit on the maximum and minimum value possible. The Integer.MAX_VALUE and Integer.MIN_VALUE will show what is the maximum and minimum possible int value than can be evaluated.
Java Code Example :
This java example source code demonstrates the use of nextInt method of Scanner class. Basically the code just prints those tokens of int data type derived from the input string declared on the Scanner constructor. The radix used on the example below is 16, means to say we have scanned data type in hexadecimal base number and then prints the equivalent in decimal format.
package com.javatutorialhq.java.tutorial.scanner; import java.util.Scanner; /* * Example java source code on the usage of * Scanner nextInt() method * program to print int tokens */ public class ScannerNextInt { public static void main(String[] args) { // initialize the scanner Scanner scan = new Scanner("33 A3 -21 -7 1A"); // tokenize the string on the constructor input while(scan.hasNext()){ // printing int tokens of base 16 System.out.println(scan.nextInt(16)); } // closing the scanner object scan.close(); } }
Sample Output :
Running the nextInt() method example source code of Scanner class will give you the following output
Exception Scenario :
Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at com.teknoscope.java.tutorial.scanner.ScannerNextInt.main(ScannerNextInt.java:21) Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at com.teknoscope.java.tutorial.scanner.ScannerNextInt.main(ScannerNextInt.java:23) Exception in thread "main" java.lang.IllegalStateException: Scanner closed at java.util.Scanner.ensureOpen(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at com.teknoscope.java.tutorial.scanner.ScannerNextInt.main(ScannerNextInt.java:26)
Similar Method :
- N/A
Suggested Reading List :
References :