Java Scanner nextLong() Method

The nextLong() is a Java Scanner class method which is used to scan the next token of the input as a long. There is two different types of Java nextLong() method which can be differentiated depending on its parameter. These are:

  • Java Scanner nextLong() Method
  • Java Scanner nextLong(int radix) Method

nextLong() Method

This is a Java Scanner class method which is used to scan the next token of the input as a long in the default radix.

nextLong(int radix) Method

This is an inbuilt method of Java Scanner class which is used to scan the next token of the input as a long in the specified radix.

Syntax

Following is the declaration of nextLong() method:

Parameter

DataTypeParameterDescriptionRequired/Optional
intradixIt is used to interpret the token as a int value.Required

Returns

The nextLong() method returns the long value scanned from the input.

Exceptions

IllegalStateException- This method throws exception if the innvocation is done after the scanner has been closed.

IllegalArgumentException- It throws this exception if the specified radix is out of range.

InputMismatchException- It throws this exception if the next token does not match the Integer regular expression, or is out of range.

Compatibility Version

Java 1.5 and above

Example 1

Output:

Enter the Number you want: 880
Output value: 388080

Example 2

Output:

Enter Value of a: 12345
Enter Value of x: 123456
Enter Value of b: 1234567
Enter Value of y: 1234
Output: 1223461

Example 3

Output:

Not Found: JavaTpoint.com
Found: 13
Not Found: +
Found: 13
Not Found: =
Not Found: 26.0
Not Found: true
Found: 123456789098765432

Example 4

Output:

21845
2869
-9557
-1877
1309

Example 5

Output:

Not Found :11
Not Found :Java
Exception in thread "main" java.lang.IllegalArgumentException: radix:2050
	at java.base/java.util.Scanner.setRadix(Scanner.java:1368)
	at java.base/java.util.Scanner.nextLong(Scanner.java:2370)
	at myPackage.ScannerNextLongExample5.main(ScannerNextLongExample5.java:11)