0

I am trying to reverse the digits an int number, but the code provided below returns an error:

Exception in thread "main" java.lang.NumberFormatException: For input string: "[null, null, null]"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:492)
    at java.lang.Integer.parseInt(Integer.java:527)
    at Solution.reverseInteger(Solution.java:21)
    at Solution.main(Solution.java:30)

I know there should be an easier way to do this question, but I chose to do it in my ways. I have been Googling for quite a bit, but I cannot find any relevant solutions to fix my problem. Can anyone help me out? Thanks!

import java.util.*;

class Solution {

    /*
     * param number: A 3-digit number.
     * return: Reversed number.
     */

    public int reverseInteger(int number) {
        // write your code here
        if (number > 1000 || number < 100) {
            return -1;
        }
        String s = Integer.toString(number);
        char[] c = s.toCharArray();
        String[] b = new String[c.length];

        for (int i = s.length() - 1, j = 0; i <= 0; i--, j++) {
            b[j] = String.valueOf(c[i]);
        }
        String h = Arrays.toString(b);
        int y = Integer.parseInt(h);
        return y;
    }

    public static void main(String[] args) {
        Solution p = new Solution();
        int ff = p.reverseInteger(102);
        System.out.println(ff);

    }
}
Jerrybibo
  • 1,315
  • 1
  • 21
  • 27
ericxxcc
  • 21
  • 4

2 Answers2

2

The problem is with your for loop.

for (int i = s.length()-1,j =0;i <=0; ...)

You set the value of i to

int i = s.length()-1;

but your for loop in only for i <=0, so it never gets entered into

and even when you do have b array, you can not just convert the reversed Array String to an int - add the below code and see.

    String h = Arrays.toString(b);
    System.out.println(h);

try iterating through the b string array instead

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
2

You actually have two issues here.

  1. Your condition in the for loop is incorrect. You start at s.length()-1 and decrement i, but you are checking for i <= 0 which initially is false which means your loop is never executed. The condition should be i >= 0.
  2. When doing int y = Integer.parseInt(h); your h is actually the string representation of an array, i.e. [2, 0, 1] (after correcting the for loop). See this related question for some more details.

I suggest getting rid of the String[] array and using a StringBuilder instead (trying to keep it close to your own idea). You can also get rid of some other parts in this case.

See my updated version:

import java.util.*;

class Solution {
    /*
     * param number: A 3-digit number.
     * return: Reversed number.
     */
    public int reverseInteger(int number) {
        // write your code here
        if (number > 1000 || number <100){
            return -1;
        }
        String s = Integer.toString(number);
        char[] c = s.toCharArray();
        StringBuilder b = new StringBuilder(c.length);

        for (int i = s.length()-1; i >= 0; i--){
            b.append(String.valueOf(c[i]));
        }

        int y = Integer.parseInt(b.toString());
        return y;
    }

    public static void main(String args[]) {
        Solution p = new Solution();
        int ff= p.reverseInteger(102);
        System.out.println(ff); // 201
    }
}
Community
  • 1
  • 1
Marvin
  • 13,325
  • 3
  • 51
  • 57