Home Java Scanner Methods hasNextLong() Java Scanner hasNextLong() Method Definition In Java, sc.hasNextLong() tests if the next token can be parsed as a 64-bit signed long (-9223372036854775808..9223372036854775807) without consuming it.
The overload hasNextLong(int radix) checks digits against a chosen base between 2 and 36.
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.hasNextLong(...).
Syntax Check 64-bit integer validity with hasNextLong() or hasNextLong(radix).
Scanner sc = new Scanner("5000000000 deadbeef");
boolean fits = sc.hasNextLong();
sc.next();
boolean fitsHex = sc.hasNextLong(16); Call What it does sc.hasNextLong()Tests if the next token fits in a signed long (current radix). sc.hasNextLong(16)Checks whether the token is a valid signed 64-bit hex number.
Like nextLong() , hasNextLong() returns false for tokens ending in L (like "10L").
Library declarations // java.util.Scanner
public boolean hasNextLong()
public boolean hasNextLong(int radix) Parameters Parameter Description radix (optional)Base from 2 to 36 used to validate the token.
Return Value Return Value Description booleantrue if a token exists and fits in long; false otherwise.Throws IllegalStateException If the scanner is closed. Throws IllegalArgumentException If radix is outside 2..36.
Compare with Similar Methods 64-bit lookahead alongside adjacent integer checks:
Java Examples 1. Classify int vs long vs huge 2. Filter 64-bit hex tokens 3. Skip pending labels Example 1 — Classify Tokens into int, long, or BigInteger Inspect each number without consuming until we know the tightest integer type that fits.
import java.util.Scanner;
public class ScannerHasNextLongClassify {
public static void main(String[] args) {
String nums = "42 5000000000 99999999999999999999";
try (Scanner sc = new Scanner(nums)) {
while (sc.hasNext()) {
if (sc.hasNextInt()) {
System.out.println("int: " + sc.nextInt());
} else if (sc.hasNextLong()) {
System.out.println("long: " + sc.nextLong());
} else if (sc.hasNextBigInteger()) {
System.out.println("BigInteger: " + sc.nextBigInteger());
} else {
System.out.println("Other: " + sc.next());
}
}
}
}
} int: 42
long: 5000000000
BigInteger: 99999999999999999999 How It Works Tiered lookahead stays put. Failing hasNextInt() leaves 5000000000 untouched for hasNextLong() to inspect next.64-bit tier catches 5 billion. hasNextLong() succeeds and nextLong() consumes 5000000000.20-digit fallback. When hasNextLong() rejects the 20-digit token, hasNextBigInteger() accepts it.Example 2 — Filter Valid Hexadecimal Tokens with hasNextLong(16) Scan mixed tokens and sum only those valid in hexadecimal base 16.
import java.util.Scanner;
public class ScannerHasNextLongHex {
public static void main(String[] args) {
String log = "ff invalid 100000000 zzz";
long sum = 0L;
try (Scanner sc = new Scanner(log)) {
while (sc.hasNext()) {
if (sc.hasNextLong(16)) {
sum += sc.nextLong(16);
} else {
System.out.println("Skipped: " + sc.next());
}
}
}
System.out.println("Hex sum in decimal: " + sum);
}
} Skipped: invalid
Skipped: zzz
Hex sum in decimal: 4294967551 How It Works Base-16 filter. ff (255) and 100000000 (2^32 = 4294967296) both pass hasNextLong(16).Non-hex words skipped. Tokens with letters outside a-f return false and are cleared by sc.next().Exact 64-bit total. Adding 255 + 4294967296 gives 4294967551 without overflowing.Example 3 — Sum Large Transaction Values While Skipping Status Text Add large account transfers while cleanly bypassing "pending" markers.
import java.util.Scanner;
public class ScannerHasNextLongTx {
public static void main(String[] args) {
String feed = "2500000000 pending 1500000000 failed 100L";
long cleared = 0L;
try (Scanner sc = new Scanner(feed)) {
while (sc.hasNext()) {
if (sc.hasNextLong()) {
cleared += sc.nextLong();
} else {
sc.next();
}
}
}
System.out.println("Cleared total: " + cleared);
}
} Cleared total: 4000000000 How It Works Ignore status words. "pending" and "failed" fail hasNextLong() and are discarded silently.L-suffix rejected. Even "100L" returns false because Java literal suffixes are invalid in text input.Combined result. The two numeric entries add cleanly to 4000000000.Frequently Asked Questions What is the maximum number hasNextLong() accepts? 9223372036854775807 (Long.MAX_VALUE), and down to -9223372036854775808 (Long.MIN_VALUE).
Does hasNextLong() return true for "42L"? No. The trailing L makes it fail integer parsing, so hasNextLong() returns false.
Can hasNextLong() check binary or hex tokens? Yes, via sc.hasNextLong(2) or sc.hasNextLong(16).
What happens if a token is too large for long? hasNextLong() simply returns false without throwing an exception.
Does hasNextLong() advance past the token? No. Call nextLong() to read it, or next() to skip it when hasNextLong() is false.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
11 people found this page helpful
Helpful Share Copy link Suggestion