I want 31842 to be stored in an array:
arr[49] would store 2
arr[48] would store 4
arr[47] would be 8
arr[46] would be 1
arr[45] would be 3
arr[0]..arr[44] would all be 0
I wrote code to do it, but an ArrayIndexOutOfBoundsException
error bumps up! Any ideas?
public static void main(String[] args)
{
Scanner scan = new Scanner (System.in);
int [] integer1 = new int[50];
int [] integer2 = new int[50];
String string;
char ch;
System.out.print("Please enter an integer #1: ");
string = scan.nextLine();
for (int i = integer1.length; i > 0; i--){
int position = string.length()-1;
ch = string.charAt(position--);
if (ch >= '0' && ch <= '9'){
int chToInt = ch - '0';
integer1[i] = chToInt;
System.out.println(integer1[0]);
}
else{
int chToInt = 0;
}
}
}