Java Scanner hasNextLong() method example

HQ ┬╗ Java Tutorial ┬╗ java.util ┬╗ Scanner ┬╗ hasNextLong() method example
Java Scanner hasNextLong() method example 2018-02-20T12:10:44+00:00

java.util.Scanner hasNextLong()

Description :

This java tutorial shows how to use the hasNextLong method of Scanner class of java.util package. This method returns boolean, true if  the token can be interpreted as long data type with respect to the radix used by the scanner object otherwise false.

Method Syntax :

There are two overloaded method of Scanner hasNextLong() java method

java scanner hasNextLong overloaded method


Note:
Overloaded methods are those method with same name but with different method signatures


Method 1:

public boolean hasNextLong()

Method 2:

public boolean hasNextLong(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 long data type.

Compatibility Version :

Requires Java 1.5 and up

Exception :

IllegalStateException – if this scanner is closed and we still invoked the hasNextLong┬ámethod

Discussion :

The Scanner hasNextLong() method simply return true if the token on the scanner buffer can be interpreter or translated of long data type. There are two overloaded method of hasNextLong method, which accomplishes the same task. The method without a method argument is the same as calling hasNextLong(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 long data type, there is limit on the maximum and minimum value possible. The Long.MAX_VALUE and Long.MIN_VALUE will show what is the maximum and minimum possible long value than can be evaluated.

Java Code Example :

This java example source code demonstrates the use of hasNextLong method of Scanner class. Basically the code just prints those tokens of long data type on the input string declared on the Scanner constructor. The radix used on the example below is radix 16, means to say we have scanned data type in hexadecimal base number and then prints the token.

Sample Output :

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

java scanner hasNextLong method example

java scanner hasNextLong 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