Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
1.1.
Syntax
1.2.
Parameter
1.3.
Returns
2.
Exceptions
3.
Examples
3.1.
Example 1
3.2.
Java
3.3.
Example 2
3.4.
Java
4.
Frequently Asked Questions
4.1.
The nextDouble() comes under which class?
4.2.
Why do we use nextDouble()?
4.3.
What is nextDouble() in Java?
4.4.
What is the return value of nextDouble?
4.5.
What does rand nextDouble do in Java?
4.6.
Has nextDouble and nextDouble?
4.7.
Is nextDouble inclusive in Java?
5.
Conclusion
Last Updated: Jun 3, 2024
Medium

Java Scanner Nextdouble() method

Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

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.

nextdouble Java

Also see, Duck Number in Java and Hashcode Method in Java.

Syntax

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

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

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

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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Examples

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();
}
}
output of example2


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.

output of example2

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(); 
}
     
}
}
}

Output

 hasNextDouble()  output

You can also find the output of this java compiler code here.

Frequently Asked Questions

The nextDouble() comes under which class?

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.

 

To learn more about DSA, competitive coding, and many more knowledgeable topics, please look into the guided paths on Coding Ninjas Studio. Also, you can enroll in our courses and check out the mock test and problems available to you. Please check out our interview experiences and interview bundle for placement preparations.

Happy Coding!

Previous article
Collections.reverseOrder() Method in Java with Examples
Next article
parseInt() Method in Java
Live masterclass