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