This should work for your purpose:
import java.util.Scanner;
public class DataType
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
if(in.hasNextByte(2))
System.out.println("Byte");
else if(in.hasNextInt())
System.out.println("Integer");
else if(in.hasNextFloat())
System.out.println("Float");
else if(in.hasNextBoolean())
System.out.println("Boolean");
else if(in.hasNext())
System.out.println("String");
}
}
Note that the order of if...else
statements is very important here because of the following set relations with respect to patterns:
- All byte patterns can be integers
- All integer patterns can be floats
- All float patterns can be Strings
- All booleans can be Strings
There are quite a lot of hasNext..()
methods in the Scanner
class, such as BigInteger
, short
, and so on. You may refer the Scanner class documentation for further details.