Integer.parse(String str) java.lang.NumberFormatException: Errors

前端 未结 8 1622
一整个雨季
一整个雨季 2020-12-11 22:18

I keep getting number format expectations, even though I\'m trimming the strings and they don\'t contain non numerical characters bizarrely it works for some numbers and not

8条回答
  •  星月不相逢
    2020-12-11 23:08

    The number you're trying to parse is larger than the max value of an Integer, you would have to use a larger data type like Long.

    public static void main(String[] args) {
        System.out.println("Integer max value: " + Integer.MAX_VALUE);
        System.out.println("Long max value: " + Long.MAX_VALUE);
        System.out.println();
    
        String n = "3020857508";
        Long a = Long.parseLong(n.trim());
        System.out.println(a);
    }
    

    Results:

    Integer max value: 2147483647
    Long max value: 9223372036854775807
    
    3020857508
    

提交回复
热议问题