In Java, user inputs play a crucial role in implementing custom functionalities. It is such that these values can be retrieved at run-time differently each time. Also, the developers find it convenient to specify a template code that can be utilized multiple times with varying values as per the requirements. The user-input double values come into effect in the case of applying mathematical calculations.
This tutorial will elaborate on implementing the Java Scanner class’s “nextDouble()” method.
How to Use the Java Scanner “nextDouble()” Method?
The “nextDouble()” method of the “java.util.Scanner” class scans the next input token as a Double.
Syntax
double nextDouble()
Return Value: This method returns a Double scanned via the input.
Thrown Exceptions
This particular method returns the following three exceptions:
- InputMismatchException: This exception is returned if the next token is not matched with the Double regular expression, or in case of being out of range.
- NoSuchElementException: It is returned if the input is confused/exhausted.
- IllegalStateException: It is given if the scanner closes.
Example 1: Applying the Java Scanner “nextDouble()” Method
This example demonstrates the usage of the stated method by returning the sum of the user input double values:
import java.util.*;
public class Nextdouble {
public static void main(String[] argv){
Scanner object = new Scanner(System.in); {
System.out.println("Enter the Double Value -> ");
double val1 = object.nextDouble();
double val2 = object.nextDouble();
double val3 = val1+val2;
System.out.println("Sum of Double Values -> " +val3);
object.close();
}
}}
In this snippet of code:
- Import the stated package to access all the features in the “java.util” package.
- Create a Scanner object where the “System.in” parameter fetches the user input.
- Now, ensure the user input values as double via the “nextDouble()” method and return their sum.
- Lastly, close the Scanner utilizing the “close()” method.
Output
Here, it can be analyzed that the user input double values are added appropriately.
Example 2: Thrown “InputMismatchException” by the “nextDouble()” Method
In this scenario, the discussed method returns the stated exception when the retrieved value is not found as a double:
import java.util.*;
public class Nextdouble {
public static void main(String[] argv){
String val = "Java";
Scanner obj = new Scanner(val);
while (obj.hasNext()) {
System.out.println("Next Double Value ->" + obj.nextDouble());
}
obj.close();
}}
According to these lines of code:
- Initialize the given string value comprising the string.
- Now, likewise, create a Scanner object.
- After that, apply the “hasNext()” method to check if there is a next value and if so, return the double value using the “nextDouble()” method.
- Since there is a defined value but is not a double, therefore, the discussed limitation will be returned instead.
Output
Example 3: Thrown “NoSuchElementException” by the “nextDouble()” Method
In this specific demonstration, the method throws this exception upon invoking the double value more times than it is contained:
import java.util.*;
public class Nextdouble {
public static void main(String[] argv){
String val = "2.0";
Scanner ob = new Scanner(val);
for (int i = 0; i < 3; i++) {
if (ob.hasNextDouble()) {
System.out.println("Double Value Founded -> " + ob.nextDouble());
}
else {
System.out.println("Double Value Not Founded-> " + ob.next());
}}
ob.close();
}}
In this snippet of code, the given double value is initialized as a string. After that, the “for” loop is applied combined with the “hasNextDouble()” and “nextDouble()” methods to invoke the double value more times than it is contained. This, in turn, yields the “NoSuchElementException” limitation.
Output
In this outcome, it can be seen that the contained double value is returned but an exception is thrown.
However, closing the Scanner before the actual implementation throws the “IllegalStateException”, demonstrated below:
Conclusion
The “nextDouble()” method of the “java.util.Scanner” class scans the next input token as a Double and throws the “InputMismatchException”, “NoSuchElementException”, and “IllegalStateException” in different discussed scenarios. This guide has explained the use of the Java “nextDouble()” method.