The
nextDouble() method of
java.util.Scanner class scans the next token of the input as a Double. If the translation is successful, the scanner advances past the input that matched.
Syntax:
public double nextDouble()
Parameters: The function does not accepts any parameter.
Return Value: This function returns the
Double scanned from the input.
Exceptions: The function throws three exceptions as described below:
- InputMismatchException: if the next token does not matches the Double regular expression, or is out of range
- NoSuchElementException: throws if input is exhausted
- IllegalStateException: throws if this scanner is closed
Below programs illustrate the above function:
Program 1:
Output:
Not found Double() value :Gfg
Found Double value :9.0
Not found Double() value :+
Found Double value :6.0
Not found Double() value :=
Found Double value :12.0
Program 2: To demonstrate InputMismatchException
Output:
Exception thrown: java.util.InputMismatchException
Program 3: To demonstrate NoSuchElementException
Output:
Not found Double value :Gfg
Exception thrown: java.util.NoSuchElementException
Program 4: To demonstrate IllegalStateException