java.util.Scanner hasNextInt()
Description :
This java tutorial shows how to use the hasNextInt method of Scanner class of java.util package. This method returns boolean, true if the token can be interpreted as int data type with respect to the radix used by the scanner object otherwise false.
Method Syntax :
There are two overloaded method of Scanner hasNextInt() java method
Note: Overloaded methods are those method with same name but with different method signatures
Method 1:
public boolean hasNextInt()
Method 2:
public boolean hasNextInt(int radix)
Parameter Input :
DataType | Parameter | Description |
---|---|---|
int | radix | the base number to scan |
Method Returns :
This method simply returns true if the scanner token is of int data type.
Compatibility Version :
Requires Java 1.5 and up
Exception :
IllegalStateException
if this scanner is closed and we still invoked the hasNextInt method
Discussion :
The Scanner hasNextInt() method simply return true if the token on the scanner buffer can be interpreter or translated of int data type. There are two overloaded method of hasNextInt method, which accomplishes the same task. The method without a method argument is the same as calling hasNextInt(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 method argument 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 hasNextInt method of Scanner class. Basically the code just prints those tokens of int data type on the input string declared on the Scanner constructor. The radix used on the example below is radix 8, means to say we have scanned data type in octal base number and then prints the equivalent in decimal format.
package com.teknoscope.java.tutorial.scanner; import java.util.Scanner; /* * Example java source code on the usage of * Scanner hasNextInt() method * Print int data type only */ public class ScannerHasNextInt { public static void main(String[] args) { // Declare File object // initialize the scanner Scanner scan = new Scanner("** Int number is 3 **"); // iterate through the tokens while(scan.hasNext()){ // print int tokens if(scan.hasNextInt(8)){ System.out.println("Found Int: "+scan.next()); } else{ scan.next(); } } scan.close(); } }
Sample Output :
Running the hasNextInt() method example source code of Scanner class will give you the following output
Exception Scenario :
Exception in thread "main" java.lang.IllegalStateException: Scanner closed at java.util.Scanner.ensureOpen(Unknown Source) at java.util.Scanner.hasNext(Unknown Source) at com.teknoscope.java.tutorial.scanner.ScannerHasNextInt.main(ScannerHasNextInt.java:30)
Similar Method :
- N/A
Suggested Reading List :
References :