The immediate problem is due to you using <= temp.length() instead of < temp.length(). However, you can achieve this a lot more simply. Even if you use the string approach, you can use:
String temp = Integer.toString(guess);
int[] newGuess = new int[temp.length()];
for (int i = 0; i < temp.length(); i++)
{
newGuess[i] = temp.charAt(i) - '0';
}
You need to make the same change to use < newGuess.length() when printing out the content too - otherwise for an array of length 4 (which has valid indexes 0, 1, 2, 3) you'll try to use newGuess[4]. The vast majority of for loops I write use < in the condition, rather than <=.
The immediate problem is due to you using <= temp.length() instead of < temp.length(). However, you can achieve this a lot more simply. Even if you use the string approach, you can use:
String temp = Integer.toString(guess);
int[] newGuess = new int[temp.length()];
for (int i = 0; i < temp.length(); i++)
{
newGuess[i] = temp.charAt(i) - '0';
}
You need to make the same change to use < newGuess.length() when printing out the content too - otherwise for an array of length 4 (which has valid indexes 0, 1, 2, 3) you'll try to use newGuess[4]. The vast majority of for loops I write use < in the condition, rather than <=.
You don't need to convert int to String. Just use % 10 to get the last digit and then divide your int by 10 to get to the next one.
int temp = test;
ArrayList<Integer> array = new ArrayList<Integer>();
do{
array.add(temp % 10);
temp /= 10;
} while (temp > 0);
This will leave you with ArrayList containing your digits in reverse order. You can easily revert it if it's required and convert it to int[].
You can convert entire string and then you get the toCharArray method separately characters in an array
Scanner t = new Scanner(System.in);
int x = t.nextInt();
char[] xd = String.valueOf(x).toCharArray();
for (int i = 0; i < xd.length; i++) {
System.out.println(xd[i]);
}
Another way of doing this would be:
int test = 12345;
int[] testArray = new int[String.valueOf(test).length()];
And then looping over it.
int x = 10382;
String[] str1 = Integer.toString(x).split("");
for(int i=0;i<str1.length;i++){
System.out.println(str1[i]);
}