-2

I am trying to run a loop to see if an int is sorted. however the int has to be converted from a string. here is my code.

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{

            }
        }
    }
}

when I run it, I get an error : 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) why does Integer.parseInt() not work

1 Answers1

0

Your problem is that digits[a+1] hasn't been defined yet. I see that on line 2 you have

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

and you're iterating over a in a for loop, so I daresay that digits[a+1] hasn't been assigned yet.

UPDATE 1

Check out this solution, it shows how to properly catch that exception and how to avoid it:

Java: Good way to encapsulate Integer.parseInt()

UPDATE 2

I decided to add a fixed version of your code:

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{

            }
        }
    }
}

While I don't know the utility of your code, but this implementation might be simpler:

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);
        }
    }
}
Community
  • 1
  • 1
SsJVasto
  • 486
  • 2
  • 13