In Java, sc.nextLong() parses the next token into a 64-bit signed long (from -9223372036854775808 to 9223372036854775807). You can also supply a radix from 2 to 36.
Use nextLong() instead of nextInt() for file sizes in bytes, account IDs, populations, or timestamps that exceed 2.1 billion.
An instance method of java.util.Scanner, available since Java 5 (JDK 1.5). A Scanner reads from any source (System.in for keyboard, a String, or a file) with identical parsing logic; pass the source to new Scanner(...) after import java.util.Scanner; and call sc.nextLong(...).
Syntax
Read base-10 large integers with nextLong(), or pass a radix such as 16 for 64-bit hex strings.
Java
Scanner sc = new Scanner("8100000000 7fffffffffffffff");
long population = sc.nextLong(); // 8100000000L
long maxVal = sc.nextLong(16); // 9223372036854775807L
Call
What it does
sc.nextLong()
Reads the next token as a long in the scanner's current radix.
sc.nextLong(16)
Parses a 64-bit hexadecimal integer token.
sc.hasNextLong()
Tests whether the next token fits in a long without throwing.
Do not put Java's literal L suffix in input text: "42L" throws InputMismatchException in base 10 because 'L' is not a base-10 digit.
Library declarations
Java
// java.util.Scanner
public long nextLong()
public long nextLong(int radix)
Parameters
Parameter
Description
radix (optional)
Number base (2 to 36) for interpreting the token.
Return Value
Return Value
Description
long
The parsed 64-bit signed integer.
Throws InputMismatchException
If the token has invalid characters or falls outside Long.MIN_VALUE..Long.MAX_VALUE.