-1

I am creating a wordle-type game for an intro to Java class, and keep encountering an index out of bounds exception on line 38, (shown with **), no matter what I seem to change. Any advice would be appreciated!

char[] letters = winner.toCharArray();
char[] getters = guess.toCharArray();
for (int q = 0; q < 6; q++){
    System.out.println("Please enter a 5 letter guess:");
        guess = in.nextLine();
for (int i = 0; i < 6; i++){
    **if (letters[i] == getters[i]){
        soFar = soFar + "||"+ getters[i] + "||";
    } else {
        soFar = soFar + getters[i];
Claire
  • 1
  • 1
  • 4
    Change `for (int i = 0; i < 6; i++){` to `for (int i = 0; i < Math.min(letters.length, getters.length); i++){` – Elliott Frisch May 19 '22 at 04:46
  • 1
    At the time `getters` variable is created, `guess` doesn't have any value from your input. Therefore `getters` will be an empty array. The changes of `guess` doesn't affect `getters`'s value because they are two different objects. You should move your `getters` declaration line to under the first for loop – Dinh Tien Loc May 19 '22 at 04:52
  • 1
    *"... no matter what I seem to change ... "*. Changing things (randomly) in hope that that will fix the bug is not the way to fix a program. What you actually need to do is to take a systematic, logical approach. Understand what the exception means in the context, and then work backwards (logically) to what flaw in your code caused it. Then figure out the correct way to fix the flaw. – Stephen C May 19 '22 at 04:54
  • Your for loop does not seem to have the closing brace. Are you sure the code even runs? MRE does not mean omitting out braces necessary for correct syntax. MRE means the shortest code, that I can copy paste into my IDE and reproduce the error, preferably without the need of any console I/O. – justanotherguy May 19 '22 at 05:19

2 Answers2

0

From the available information of your post, let's assume your code would work something similar to function below -

public static void main(String[] args) {

    String guess = "";
    char[] letters = "winner".toCharArray();
    char[] getters = guess.toCharArray();
    for (int q = 0; q < 6; q++){
        System.out.println("Please enter a 5 letter guess:");
            guess = "test";
    for (int i = 0; i < 6; i++){
        if (letters[i] == getters[i]){
            System.out.println(letters[i]);
        } 
    }
    }
}

The problem here is char[] getters is initialized to whatever char array was backing the String guess. But later changing the guess String isn't going to assign the new reference to char[] getters. So when you are iterating over array you would then hit java.lang.ArrayIndexOutOfBoundsException

If you are absolutely sure that char[] letters char[] getters will both be of size 6, then something like this would help -

guess = in.nextLine();
getters = guess.toCharArray();

So essentially -

char[] letters = winner.toCharArray();
char[] getters = guess.toCharArray();
for (int q = 0; q < 6; q++){
    System.out.println("Please enter a 5 letter guess:");
        guess = in.nextLine();
        getters = guess.toCharArray();

for (int i = 0; i < 6; i++){
    if (letters[i] == getters[i]){
        soFar = soFar + "||"+ getters[i] + "||";
    } else {
        soFar = soFar + getters[i];
0

An IndexOutOfBondsException appears whenever you try to access the empty or non-allocated index of any array (char[], int[], ...). Here is a great example for your case.

// Your array contains 5 elements
char[] arr = new char[] {'a', 'b', 'c', 'd', 'e'};
// Then, you invoke an element that doesn't exist in an array
System.out.println(arr[5]); // This invoking will throw an IndexOutOfBondsException

// Even you invokes like this
System.out.println(arr[-1]); // Not exist
System.out.println(arr.size()); // Because the size is 5

Now staring at your code, it is a little bit ambiguous because the guess variable is an unknown type. Assume guess is a String and you are trying to put a guess as a String into a getters. Then you set a guess value as an inputted value. This thing is ambiguous and causes a wrong logic code and the getters still be an empty array (or not contain 6 elements).

SOLUTION In order to solve it, you need to put the line char[] getters = guess.toCharArray(); below guess = in.nextLine();. It means, that after taking a guess value from the user, you create an array that contains a guess character. Here is an instance of what I describe:

char[] letters = winner.toCharArray();
// Take value from input
for (int q = 0; q < 6; q++) {
    System.out.println("Please enter a 5 letter guess:");
    guess = in.nextLine();
}
        
// Then put it into a getters array
char[] getters = guess.toCharArray();
        
// Trying to compare
for (int i = 0; i < 6; i++) {
    if (letters[i] == getters[i]) {
        soFar = soFar + "||" + getters[i] + "||";
    } else {
        soFar = soFar + getters[i];
    }
}