how to check if parsing in integer is possible in java

Solutions on MaxInterview for how to check if parsing in integer is possible in java by the best coders in the world

showing results for - "how to check if parsing in integer is possible in java"
Lukas
20 Mar 2016
1public static boolean isParsable(String input) {
2    try {
3        Integer.parseInt(input);
4        return true;
5    } catch (final NumberFormatException e) {
6        return false;
7    }
8}
Andrea
16 Mar 2019
1// if parse is possible then it will do it otherwise compiler 
2// will throw exceptions and it is a bad practice to catch all exceptions
3// in this case only NumberFormatException happens
4public boolean isInteger(String string) {
5    try {
6        Integer.valueOf(string);
7        // Integer.parse(string) /// it can also be used
8        return true;
9    } catch (NumberFormatException e) {
10        return false;
11    }
12}
13