Java Scanner hasNextInt() method example

HQ » Java Tutorial » java.util » Scanner » hasNextInt() method example
Java Scanner hasNextInt() method example 2018-02-20T12:09:49+00:00

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

java scanner hasNextint overload 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.

Sample Output :

Running the hasNextInt() method example source code of Scanner class will give you the following output

java scanner hasNextInt method example

java scanner hasNextInt method example

Exception Scenario :

Similar Method :

  • N/A

Suggested Reading List :

References :

 

Contact Info

Singapore

Our Goal

JavaTutorialHQ aims to to be The Ultimate Guide on Java with hundreds of examples from basic to advance Topics.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close