How to prevent string input in Java

1 Answer

0 votes
import java.math.BigDecimal;

public class MyClass {
    public static void main(String args[]) {
        String s1 = "82734"; 
        String s2 = "java";

        try {
            int i = Integer.parseInt(s1);
        }
        catch(NumberFormatException e) { 
            System.out.println(s1 + " is not a valid Integer");
            System.out.println(e);
        }
        try 
        { 
            new BigDecimal(s2);
        }  
        catch (NumberFormatException e) 
        { 
            System.out.println(s2 + " is not a valid Integer");
            System.out.println(e);
        } 
    }
}



/*
run:

java is not a valid Integer
java.lang.NumberFormatException: Character j is neither a decimal digit number, decimal point, nor "e" notation exponential mark.

*/

 



answered Aug 21, 2019 by avibootz
...