Scanner hasNextFloat() Method in Java

hasNextFloat method in java

If we are required to check whether or not, the input by the user is of float type, then we can use this method, hasNextFloat. Well, this method returns true, if the given user input can be interpreted as float-type data. Otherwise, the method returns false.

Checking if the user input is float type(hasNextFloat method)

Let’s have a look at an example, where we try demonstrating the hasNextFloat method.

package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean storesfloat = scanner.hasNextFloat();
System.out.println(storesfloat);
}
}

If you try to implement the above program and give some floating type input, the method is going to return true, else it is going to return false for any other input.

package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean storesfloat = scanner.hasNextFloat();
System.out.println(storesfloat);
}
}

So, if you try implementing the above program and give some floating type input, the method is going to return true, else it is going to return false.