how to check whether string is long or double code example

Example 1: test if string is float java

public static void main(String[] args) {
    String str = "5588";
    //check if int
    try{
        Integer.parseInt(str);
    }catch(NumberFormatException e){
        //not int
    }
    //check if float
    try{
        Float.parseFloat(str);
    }catch(NumberFormatException e){
        //not float
    }
}

Example 2: check if string is decimal java

Scanner in = new Scanner(System.in);
		
		String ip1 = in.nextLine();
		
		if( ip1.matches("^\\d+\\.\\d+") ) 
			System.out.println(ip1+"----is a decimal number");
		else
			System.out.println(ip1+"----is a not decimal number");

Tags:

Java Example