🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The nextDouble() method in Java, part of the java.util.Scanner class, is used to retrieve the next token from the input as a double value. This method is useful for reading and processing floating-point values from the input.
Table of Contents
- Introduction
nextDouble()Method Syntax- Understanding
nextDouble() - Examples
- Basic Usage
- Handling Input Errors
- Real-World Use Case
- Conclusion
Introduction
The nextDouble() method returns the next token from the scanner's input as a double. This method is useful when you need to read and process floating-point values, which represent decimal numbers.
nextDouble() Method Syntax
The syntax for the nextDouble() method is as follows:
public double nextDouble()
Parameters:
- This method does not take any parameters.
Returns:
- The next token as a
doublevalue.
Throws:
InputMismatchException: If the next token does not match thedoubleregular expression, or is out of range.NoSuchElementException: If no more tokens are available.IllegalStateException: If the scanner is closed.
Understanding nextDouble()
The nextDouble() method retrieves the next token and converts it to a double. If the token cannot be interpreted as a double, an InputMismatchException is thrown.
Examples
Basic Usage
To demonstrate the basic usage of nextDouble(), we will create a Scanner object and use it to read double values from a string.
Example
import java.util.Scanner;
public class NextDoubleExample {
public static void main(String[] args) {
String input = "10.5 20.5 30.5";
// Create Scanner object in try-with-resources to ensure it closes automatically
try (Scanner scanner = new Scanner(input)) {
while (scanner.hasNextDouble()) {
double value = scanner.nextDouble();
System.out.println("Double value: " + value);
}
} // Scanner is automatically closed here
}
}
Output:
Double value: 10.5
Double value: 20.5
Double value: 30.5
Handling Input Errors
This example shows how to handle errors when the input token cannot be interpreted as a double.
Example
import java.util.InputMismatchException;
import java.util.Scanner;
public class HandleInputErrorsExample {
public static void main(String[] args) {
String input = "10.5 abc 20.5";
// Create Scanner object in try-with-resources to ensure it closes automatically
try (Scanner scanner = new Scanner(input)) {
while (scanner.hasNext()) {
try {
double value = scanner.nextDouble();
System.out.println("Double value: " + value);
} catch (InputMismatchException e) {
System.out.println("Invalid input: " + scanner.next());
}
}
} // Scanner is automatically closed here
}
}
Output:
Double value: 10.5
Invalid input: abc
Double value: 20.5
Real-World Use Case
Reading Configuration Data
In real-world applications, the nextDouble() method can be used to read and process floating-point configuration data from a file.
Example
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ConfigParser {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(new File("config.txt"))) {
while (scanner.hasNext()) {
try {
double value = scanner.nextDouble();
System.out.println("Configuration value: " + 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 config.txt contains valid and invalid double values):
Configuration value: 10.5
Invalid input: abc
Configuration value: 20.5
...
Conclusion
The Scanner.nextDouble() method is used to retrieve the next token from the input as a double value. This method is particularly useful for applications requiring floating-point input values. By understanding and using this method, you can efficiently parse and handle double input data. Always close the Scanner using try-with-resources to ensure proper resource management.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment