How to check whether input value is integer or float?
Suppose 312/100=3.12 Here i need check whether 3.12 is a float value or integer value, i.e., without any decimal
You can use Scanner Class to find whether a given number could be read as Int or Float type.
import java.util.Scanner;
public class Test {
public static void main(String args[] ) throws Exception {
Scanner sc=new Scanner(System.in);
if(sc.hasNextInt())
System.out.println("This input is of type Integer");
else if(sc.hasNextFloat())
System.out.println("This input is of type Float");
else
System.out.println("This is something else");
}
}
Also:
(value % 1) == 0
would work!