Java - 使用.parseInt(String)时的NumberFormatException

时间:2017-03-21 14:23:35

标签: java arrays loops for-loop

我正在尝试运行循环以查看int是否已排序。但是必须从字符串转换int。这是我的代码。

public static void main(String[] args) {
    // TODO code application logic here
    Scanner maxVal = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
    System.out.println("enter the max value of ordered squares:");
    int max = maxVal.nextInt();
    for(int i = 0; i*i <= max; i++){
        int L = String.valueOf(i*i).length();
        String sq = String.valueOf(i*i);
        String [] digits = new String[L];
        for(int a = 0; a < L; a++){
            digits [a] = Character.toString(sq.charAt(a));
            if(L == 1){
                System.out.print(sq + "");
            }else if(Integer.parseInt(digits [a]) < Integer.parseInt(digits[a+1])){
                System.out.print(sq);
            }else{

            }
        }
    }
}

当我运行它时,我收到一个错误: Exception in thread "main" java.lang.NumberFormatException: null 0149 at java.lang.Integer.parseInt(Integer.java:542) at java.lang.Integer.parseInt(Integer.java:615) 为什么Integer.parseInt()不起作用

1 个答案:

答案 0 :(得分:0)

您的问题是digits[a+1]尚未定义。我在第2行看到你有

digits[a] = Character.toString(sq.charAt(a));

并且您在a循环中迭代for,所以我敢说digits[a+1]尚未分配。

更新1

查看此解决方案,它显示了如何正确捕获该异常以及如何避免它:

Java: Good way to encapsulate Integer.parseInt()

更新2

我决定添加一个固定版本的代码:

public static void main(String[] args) {
    // TODO code application logic here
    Scanner maxVal = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
    System.out.println("enter the max value of ordered squares:");
    int max = maxVal.nextInt();
    for(int i = 0; i*i <= max; i++){
        int L = String.valueOf(i*i).length();
        String sq = String.valueOf(i*i);
        String [] digits = new String[L];
        for(int a = 0; a < L; a++){
            digits [a] = Character.toString(sq.charAt(a));
            if(L == 1 || a == 0){
                System.out.print(sq + "");
            }else if(Integer.parseInt(digits [a]) < Integer.parseInt(digits[a+1])){
                System.out.print(sq);
            }else{

            }
        }
    }
}

虽然我不知道您的代码的实用程序,但这种实现可能更简单:

public static void main(String[] args) {
    // TODO code application logic here
    Scanner maxVal = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
    System.out.println("enter the max value of ordered squares:");
    int max = maxVal.nextInt();

    for(int i = 0; i*i <= max; i++){
        long sq = i*i;
        if(sq > 9){
            String[] digits = sq.toString().split("");

            //Notice that I start at index 1, so I can do [a-1] safely
            for(int a = 1; a < digits.length; a++){
                if(Integer.parseInt(digits [a-1]) < Integer.parseInt(digits[a])){
                    System.out.print(sq);
                    //I guess we don't want a number like 169 (13*13) to be displayed twice, so:
                    break;
                }
            }
        } else {
            System.out.print(sq);
        }
    }
}