I've made a little block of code that has the goal of attempting to store all the digits of a number into an array. For example, the number "123" would be stored as {1,2,3}. Everything seems to work fine, except for when the length of the number is larger then 10. Is it something wrong with my method? The exact error message is
Exception in thread "main" java.lang.NumberFormatException: For input string: "1202020202020202020" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.base/java.lang.Integer.parseInt(Integer.java:652) at java.base/java.lang.Integer.parseInt(Integer.java:770) at test.main(test.java:8)
public class test {
public static void main(String[] args){
//This block of code parses the zipcode and stores each number it has into an array. It also fetches the length, which is used later.
String input = args[0];
int length = input.length();
int zipcode = Integer.parseInt(args[0]);
int[] digits = new int[length];
for(int i = 0; i < length ; i++){
digits[i] = zipcode % 10;
zipcode = zipcode /10;
}
}
}
For input string: "n"... you appear to be inputting text somehow. I see nothing wrong with your logic, and it should work for a number string of arbitrary length. - Tim Biegeleisenint, so you can't useintorparseInt(). You should just count the characters, which.length()already does, and make sure each one is numeric, if that's your requirement. It isn't in UK for example. - user207421