0

I'm trying to print out a char array in my program, but in my console, the array shows up as 5 boxes.

My code is a guessing game, taking a letter and scanning the inputArray (char) for a match. If there is a match, it adds the correctly guessed letter to the corresponding position in the currentGuessArray. Am I not populating the array correctly?

for (int i = 0; i == lengthOfWord; i++) {
    if (guess.charAt(0) == inputArray[i]) {
        currentGuessArray[i] = guess.charAt(0);
    }
}
System.out.println(currentGuessArray);

This is what I am currently outputting

Current console ouput

My full code is

public class Console {
 static String input = "";

public static String get() {
    @SuppressWarnings("resource")
    System.out.println("Enter the word to guess");
    Scanner s = new Scanner(System.in);
    input = s.nextLine();
    return input;
      }
   }


public class WordGuess {

 public static void guess() {

    Scanner s = new Scanner(System.in);
    String guess;
    int trys = 0;

    String input = ConsoleView.input;
    int length = input.length();
    char[] inputArray = input.toCharArray();

    boolean[] currentGuess = new boolean[length];
    char[] currentGuessArray = new char[length];

    while (currentGuessArray != inputArray) {
        System.out.println("Key in one character or your guess word:");
        trys++;
        guess = s.nextLine();
        int guessLength = guess.length();


        if (guessLength == 1) {
            for (int i = 0; i == length; i++) {
                if (guess.charAt(0) == inputArray[i]) {
                    //currentGuess[i] = true;
                    currentGuessArray[i] = guess.charAt(0);
                }

            }
            System.out.println(Arrays.toString(currentGuessArray));

        } else if (guess.equals(input)) {
            System.out.println("You're correct!");
            break;
        }

        else if (currentGuessArray == inputArray) {
            System.out.println("Congratulations, you got the word in " + trys);
        }
    }

   }
}

3 Answers 3

1

It's not working because your for loop is never looping:

for (int i = 0; i == length; i++)

The second expression in a for is the condition under which looping will continue. So in this case, i == length is immediately false, and the loop never runs, which is why your array currentGuessArray has bad values in it.

Change it to this:

for (int i = 0; i < length; i++)

and you should be okay on that one.

By the way, your while loop also will execute forever. (currentGuessArray != inputArray) is always false because these are references, and != compares references. You'll need to compare the elements of the arrays to see if they're the same. (I'm pretty sure there's a method in Arrays that can do that.)

Sign up to request clarification or add additional context in comments.

1 Comment

Haha, wow I can't believe I overlooked such a tiny mistake! Thanks, this was the fix!
0

If you want print char array in String representation, you need to create String object from your char array.

Try this:

String.valueOf(currentGuessArray);

3 Comments

Unfortunately I've tried that (and .toString) but still get the same result?
This seems like an encoding problem. Show us the full code.
I have added my full code, since I still cannot get a valid return.
0

You can try Arrays.toString()

System.out.println(Arrays.toString(currentGuessArray));

Also, make sure you're using the alphabet characters and not other random characters.

You can find out like this

for (char c : currentGuessArray) {
    if (!Character.isLetter(c)) {
        System.out.println("Error !, currentGuessArray contains other characters than letters);
    }
}

1 Comment

I tried this, and the test showed my array contains alphabet characters, but the toString suggestion still didn't work.

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.