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);
}
}