snazzyjazzy
snazzyjazzy

Reputation: 49

Printing char array outputs empty boxes?

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);
        }
    }

   }
}

Upvotes: 0

Views: 2286

Answers (3)

Tyrel
Tyrel

Reputation: 737

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.)

Upvotes: 1

Yassin Hajaj
Yassin Hajaj

Reputation: 21975

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);
    }
}

Upvotes: 0

Aleksandr Podkutin
Aleksandr Podkutin

Reputation: 2580

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

Try this:

String.valueOf(currentGuessArray);

Upvotes: 0

Related Questions