1

I have a method called displayWord that is supposed to compare each index of an array with another array and if the indexes match, it is supposed to execute this line displayedWord[i] = wordArray[i].

When I print the displayedWord, they are all question marks even thought the print statement executes so I know it is going into the if block.

Why is displayedWord always questions marks when I print it?

public static void displayWord(char[] correctGuesses, char[] wordArray) {
        char[] displayedWord = new char[wordArray.length];
        for(int i = 0; i < wordArray.length; i++) {
            for(int j = 0; j < correctGuesses.length; j++) {
                if(wordArray[i] == correctGuesses[j]) {
                    displayedWord[i] = wordArray[i];
                    System.out.println("they are the same");
                } else displayedWord[i] = '?';
            }
        }
        for(char c : displayedWord) {
            System.out.print(c);
        }
    }
7
  • 2
    Add a break; inside the if block (after System.out.println("they are the same");) Commented Jun 26, 2022 at 22:05
  • That ended up in weird behavior as the overall programs prompts the user again after executing this method. Commented Jun 26, 2022 at 22:06
  • Do correctGuesses and wordArray have the same length? Commented Jun 26, 2022 at 22:07
  • Are you intentionally comparing each element of wordArray with every element of correctGuesses, rather than just the element with the same index? Step through your program with the debugger. Commented Jun 26, 2022 at 22:11
  • They are the same length and I am intentionally comparing each element of wordArray with every element in correctGuesses. About to step through with debugger. Commented Jun 26, 2022 at 22:11

3 Answers 3

3

That's because you are overeating the displayedWord, even if the char was found

Use break when you find the char to get out of the loop

here is the code

public static void displayWord(char[] correctGuesses, char[] wordArray) {
    char[] displayedWord = new char[wordArray.length];
    for(int i = 0; i < wordArray.length; i++) {
        for(int j = 0; j < correctGuesses.length; j++) {
            if(wordArray[i] == correctGuesses[j]) {
                displayedWord[i] = wordArray[i];
                System.out.println("they are the same");
                break;
            } else displayedWord[i] = '?';
        }
    }
    for(char c : displayedWord) {
        System.out.print(c);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Currently you are not comparing the corresponding indices of the two arrays, because you have defined a double for loop with two different variables. With your code above it loops through each element of wordArray and compares every element of correctGuesses array for each wordArray element.

To get what you want, you only want to have one for loop which iterates n-times where n is the length of the smaller array.

Comments

0

You are overriding the correct character at the next iteration when your code hits the else case.

And it also seems like you don't need a nested loop to check whether a character in the array of guesses a matches a character in the containing correct characters (in case if characters at the same position need to be compared).

public static void displayWord(char[] correctGuesses, char[] wordArray) {
    int len = Math.min(correctGuesses.length, wordArray.length);
    char[] displayedWord = new char[len];
    for(int i = 0; i < len; i++) {
        if(wordArray[i] == correctGuesses[i]) {
            displayedWord[i] = wordArray[i];
            System.out.println("they are the same " + wordArray[i]);
        } else displayedWord[i] = '?';
    }
    
    for(char c : displayedWord) {
        System.out.print(c);
    }
}

main()

public static void main(String[] args) {
    displayWord(new char[]{'a', 'b', 'c'},new char[]{'e', 'b', 'e'});
}

Output:

they are the same b
?b?

4 Comments

I could be wrong but doesnt this compare only the same indexed element in correctGuesses rather than every element in correctGuesses?
@guts716 You need to find a matching character at same position in both array wordArray and correctGuesses? If the answer is yes, then you need only one loop.
@guts716 If a matching character in the wordArray can be located at any position, that means that the same character wordArray can match multiple characters in the correctGuesses array (i.e. a single a in {'a', 'b', 'c'} would match two characters in the {'a', 'u', 'a'})
@guts716 Which of these was your original intention? The first option is represented in the example in my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.