The nextLong()
method in Java, part of the java.util.Scanner
class, is used to retrieve the next token from the input as a long
value. This method is useful for reading and processing long integer values from the input.
Table of Contents
- Introduction
nextLong()
Method Syntax- Understanding
nextLong()
- Examples
- Basic Usage
- Using a Custom Radix
- Handling Input Errors
- Real-World Use Case
- Conclusion
Introduction
The nextLong()
method returns the next token from the scanner's input as a long
. This method is useful when you need to read and process long integer values.
nextLong() Method Syntax
There are two versions of the nextLong()
method:
Default Radix
public long nextLong()
Custom Radix
public long nextLong(int radix)
Parameters:
radix
: The radix (base) to be used for interpreting the token as along
.
Returns:
- The next token as a
long
value.
Throws:
InputMismatchException
: If the next token does not match thelong
regular expression, or is out of range.NoSuchElementException
: If no more tokens are available.IllegalStateException
: If the scanner is closed.
Understanding nextLong()
The nextLong()
method retrieves the next token and converts it to a long
. If the token cannot be interpreted as a long
, an InputMismatchException
is thrown.
Examples
Basic Usage
To demonstrate the basic usage of nextLong()
, we will create a Scanner
object and use it to read long
values from a string.
Example
import java.util.Scanner;
public class NextLongExample {
public static void main(String[] args) {
String input = "1234567890123 9876543210987 123456789012345";
// Create Scanner object in try-with-resources to ensure it closes automatically
try (Scanner scanner = new Scanner(input)) {
while (scanner.hasNextLong()) {
long value = scanner.nextLong();
System.out.println("Long value: " + value);
}
} // Scanner is automatically closed here
}
}
Output:
Long value: 1234567890123
Long value: 9876543210987
Long value: 123456789012345
Using a Custom Radix
This example shows how to use nextLong(int radix)
to read long
values with a specific radix.
Example
import java.util.Scanner;
public class NextLongWithRadixExample {
public static void main(String[] args) {
String input = "1100100 1111111";
// Create Scanner object in try-with-resources to ensure it closes automatically
try (Scanner scanner = new Scanner(input)) {
while (scanner.hasNextLong(2)) { // Check for binary numbers (base 2)
long value = scanner.nextLong(2);
System.out.println("Long value (binary): " + value);
}
} // Scanner is automatically closed here
}
}
Output:
Long value (binary): 100
Long value (binary): 127
Handling Input Errors
This example shows how to handle errors when the input token cannot be interpreted as a long
.
Example
import java.util.InputMismatchException;
import java.util.Scanner;
public class HandleInputErrorsExample {
public static void main(String[] args) {
String input = "1234567890123 abc 9876543210987";
// Create Scanner object in try-with-resources to ensure it closes automatically
try (Scanner scanner = new Scanner(input)) {
while (scanner.hasNext()) {
try {
long value = scanner.nextLong();
System.out.println("Long value: " + value);
} catch (InputMismatchException e) {
System.out.println("Invalid input: " + scanner.next());
}
}
} // Scanner is automatically closed here
}
}
Output:
Long value: 1234567890123
Invalid input: abc
Long value: 9876543210987
Real-World Use Case
Reading Large Numerical Data
In real-world applications, the nextLong()
method can be used to read and process large numerical data that cannot be handled by primitive integer types.
Example
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class LargeDataParser {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(new File("large_numbers.txt"))) {
while (scanner.hasNext()) {
try {
long value = scanner.nextLong();
System.out.println("Large number: " + value);
} catch (InputMismatchException e) {
System.out.println("Invalid input: " + scanner.next());
}
}
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.getMessage());
} // Scanner is automatically closed here
}
}
Output (Assuming large_numbers.txt
contains valid and invalid long values):
Large number: 1234567890123456789
Invalid input: abc
Large number: 9876543210987654321
...
Conclusion
The Scanner.nextLong()
method is used to retrieve the next token from the input as a long
value. This method is particularly useful for applications requiring large integer input values. By understanding and using this method, you can efficiently parse and handle long input data. Always close the Scanner
using try-with-resources to ensure proper resource management.
Comments
Post a Comment
Leave Comment