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 parameter.

Return Value: This function returns true if and only if this scanner’s next token is a valid Float.

Exceptions: The function throws IllegalStateException if this scanner is closed.



Below programs illustrate the above function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate the
// hasNextFloat() 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 Floats in a string
        scanner.useLocale(Locale.US);
  
        // iterate till end
        while (scanner.hasNext()) {
  
            // check if the scanner's
            // next token is a Float with the default radix
            System.out.print("" + scanner.hasNextFloat());
  
            // print what is scanned
            System.out.print(" -> " + scanner.next() + "\n");
        }
  
        // close the scanner
        scanner.close();
    }
}

chevron_right


Output:

false -> gfg
true -> 2
false -> geeks!

Program 2: Program to demonstrate exception

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate the
// hasNextFloat() 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 Floats in a string
            scanner.useLocale(Locale.US);
  
            scanner.close();
  
            // iterate till end
            while (scanner.hasNext()) {
  
                // check if the scanner's
                // next token is a Float with the default radix
                System.out.print("" + scanner.hasNextFloat());
  
                // print what is scanned
                System.out.print(" -> " + scanner.next() + "\n");
            }
  
            // close the scanner
            scanner.close();
        }
        catch (IllegalStateException e) {
            System.out.println("Exception: " + e);
        }
    }
}

chevron_right


Output:

Exception: java.lang.IllegalStateException: Scanner closed

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#hasNextFloat()

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.