Scanner hasNextDouble() method in Java with Examples Last Updated : 16 Oct, 2018 Comments Improve Suggest changes Like Article Like Report The hasNextDouble() method of java.util.Scanner class returns true if the next token in this scanner's input can be interpreted as a Double using the nextDouble() method. The scanner does not advance past any input. Syntax: public Double hasNextDouble() Parameters: The function does not accepts any parameter. Return Value: This function returns true if and only if this scanner's next token is a valid Double. Exceptions: The function throws IllegalStateException if this scanner is closed. Below programs illustrate the above function: Program 1: Java // Java program to illustrate the // hasNextDouble() method of Scanner class in Java // without parameter import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { String s = "gfg 2 geeks!"; // new scanner with the // specified String Object Scanner scanner = new Scanner(s); // use US locale to interpret Doubles in a string scanner.useLocale(Locale.US); // iterate till end while (scanner.hasNext()) { // check if the scanner's // next token is a Double with the default radix System.out.print("" + scanner.hasNextDouble()); // print what is scanned System.out.print(" -> " + scanner.next() + "\n"); } // close the scanner scanner.close(); } } Output: false -> gfg true -> 2 false -> geeks! Program 2: Program to demonstrate exception Java // Java program to illustrate the // hasNextDouble() method of Scanner class in Java // Exception case import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { String s = "gfg 2 geeks!"; // new scanner with the // specified String Object Scanner scanner = new Scanner(s); // use US locale to interpret Doubles in a string scanner.useLocale(Locale.US); scanner.close(); // iterate till end while (scanner.hasNext()) { // check if the scanner's // next token is a Double with the default radix System.out.print("" + scanner.hasNextDouble()); // print what is scanned System.out.print(" -> " + scanner.next() + "\n"); } // close the scanner scanner.close(); } catch (IllegalStateException e) { System.out.println("Exception: " + e); } } } Output: Exception: java.lang.IllegalStateException: Scanner closed Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#hasNextDouble() Comment More infoAdvertise with us Next Article Scanner hasNextDouble() method in Java with Examples G gopaldave Follow Improve Article Tags : Java Java - util package Java-Functions Java-Scanner Practice Tags : Java Similar Reads Scanner hasNextLine() method in Java with Examples The hasNextLine() method of java.util.Scanner class returns true if there is another line in the input of this scanner. This method may block while waiting for input. The scanner does not advance past any input. Syntax: public boolean hasNextLine() Parameters: The function does not accepts any param 2 min read Scanner hasNextByte() method in Java with Examples The hasNextByte() method of java.util.Scanner class returns true if the next token in this scanner's input can be assumed as a Byte value of the given radix. The scanner does not advance past any input. In case no radix is passed as a parameter, the function interprets the radix to be default radix 3 min read Scanner hasNextBoolean() method in Java with Examples The hasNextBoolean() method of java.util.Scanner class returns true if the next token in this scanner's input can be interpreted as a Boolean using the nextBoolean() method. The scanner does not advance past any input. Syntax: public boolean hasNextBoolean() Parameters: The function does not accepts 2 min read Scanner hasNextLong() method in Java with Examples The hasNextLong() method of java.util.Scanner class returns true if the next token in this scanner's input can be assumed as a Long value of the given radix. The scanner does not advance past any input. In case no radix is passed as a parameter, the function interprets the radix to be default radix 3 min read Scanner hasNextFloat() method in Java with Examples The hasNextFloat() method of java.util.Scanner class returns true if the next token in this scanner's input can be interpreted as a Float using the nextFloat() method. The scanner does not advance past any input. Syntax: public Float hasNextFloat() Parameters: The function does not accepts any param 2 min read Like