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.

package com.teknoscope.java.tutorial.scanner;

import java.util.Scanner;

/*
 * Example java source code on the usage of
 * Scanner hasNextLong() method
 * Print long data type tokens only
 */

public class ScannerHasNextLong {

	public static void main(String[] args) {

		// initialize the scanner
		Scanner scan = new Scanner("xxx long number is AE xxx");
		// iterate through scanner tokens
		while(scan.hasNext()){
			// print long tokens
			if(scan.hasNextLong(16)){
				System.out.println("Found Long: "+scan.next());
			}
			else{
				scan.next();
			}

		}
		// close the scanner
		scan.close();
	}
}

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 :

Exception in thread "main" java.lang.IllegalStateException: Scanner closed
	at java.util.Scanner.ensureOpen(Unknown Source)
	at java.util.Scanner.hasNext(Unknown Source)
	at java.util.Scanner.hasNextLong(Unknown Source)
	at com.teknoscope.java.tutorial.scanner.ScannerHasNextLong.main(ScannerHasNextLong.java:30)

Similar Method :

  • N/A

Suggested Reading List :

References :