Java Scanner.hasNextDouble()

Syntax

Scanner.hasNextDouble() has the following syntax.

public boolean hasNextDouble()

Example

In the following code shows how to use Scanner.hasNextDouble() method.


import java.util.Locale;
import java.util.Scanner;
// www  . j  a v a2  s  .  c o m
public class Main {

   public static void main(String[] args) {

      String s = "java2s.com 1 + 1 = 2.0 1.3985";

      Scanner scanner = new Scanner(s);

      // assign locale as US to recognize double numbers in a string
      scanner.useLocale(Locale.US);
      
      while (scanner.hasNext()) {
         // check if the scanner's next token is a double
         System.out.println(scanner.hasNextDouble());
         System.out.println(scanner.next());
      }
      scanner.close();
   }
}

The code above generates the following result.