Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
The nextDouble() method in Java's Scanner class is a powerful tool for handling input from the user or a file as double-precision floating-point values. As part of the java.util package, Scanner provides various methods to parse input tokens into different data types, and nextDouble() specifically reads the next token from the input as a double. Understanding how to use nextDouble() effectively is crucial for building robust and versatile Java applications that can handle numeric input accurately.
Syntax of Java nextDouble() method
The syntax of the nextDouble() method is straightforward:
public double nextDouble()
This indicates that the method is public, returns a double-precision floating-point value (double), and does not take any parameters.
Parameter of nextDouble() in Java
The nextDouble() method does not accept any parameters. It simply reads the next token from the input stream and attempts to parse it as a double-precision floating-point value.
Returns of nextDouble() in Java
The nextDouble() method returns the next double value from the input stream. If the input token cannot be parsed as a valid double value, the method will throw an InputMismatchException.
Exceptions of nextDouble() in Java
The nextDouble() method may throw an InputMismatchException if the next token cannot be parsed as a valid double value. This could occur if the input token is not a valid representation of a double, such as containing alphabetic characters or special symbols. It's essential to handle this exception to gracefully manage unexpected input and prevent program crashes.
Examples of Java nextDouble()
Assuming decimal data needs to be analyzed and requires precision, we will consider the double data type to store the result or outcome in this scenario with the help of nextdouble java method to read the double value. But if our priority is memory, we will use float data type.
Example 1
Java
Java
import java.util.*; public class Main { public static void main(String[] args) { System.out.println("Enter the Value: "); /*initializing a Scanner class object */ Scanner obj = new Scanner(System.in);
/*Storing the user input in a doublevalue variable*/ Double doublevalue = obj.nextDouble(); } }
You can also try this code with Online Java Compiler
But if we try to enter a value that cannot be translated into a Double value, the compile will show an InputMismatchException in the console. This means the inserted value cannot be considered a Double data type.
For example: if we enter the value coder as user input, we will get the below instructions.
What if you want to look for a Double value in a sentence? In this scenario, we can use the hasNextDouble() method in the program. The hasNextDouble() will go through the sentence and check whether it has a Double value after each sentence's whitespace.
Example 2
Java
Java
import java.util.*; public class Main { public static void main(String[] args) { String sentence = "Coding Ninjas 2.0"; Scanner obj = new Scanner(sentence);
/* checking until while statement is true*/ while(obj.hasNext()) { /* checking if the string has a double value or not*/ if(obj.hasNextDouble()) { /* printing the double value*/ System.out.println("Double-value: " + obj.nextDouble()); } else {
/* moving to the next word in sentence*/ obj.next(); }
} } }
You can also try this code with Online Java Compiler
The nextDouble() method comes under the Scanner class in java.
Why do we use nextDouble()?
To read the Double floating point numbers from the user as input.
What is nextDouble() in Java?
nextDouble() in Java is a method in the Scanner class used to read the next token from the input as a double-precision floating-point value.
What is the return value of nextDouble?
The return value of nextDouble() is the next double value from the input stream.
What does rand nextDouble do in Java?
In Java, rand.nextDouble() generates a random double value between 0.0 (inclusive) and 1.0 (exclusive).
Has nextDouble and nextDouble?
Yes, Java has both nextDouble() method in the Scanner class and nextDouble() method in the Random class for generating random doubles.
Is nextDouble inclusive in Java?
No, nextDouble() in Java is not inclusive; it reads the next token as a double but does not include the endpoint value.
Conclusion
In this blog, we discussed the Java Scanner Nextdouble() method. Mastering the nextDouble() method in Java's Scanner class is essential for efficiently handling double input. Understanding its syntax, return value, and usage scenarios empowers developers to build robust and versatile Java applications.
To learn more about java, check out the following articles.