I am building a text only hangman game and I have gotten it to the final stages as far as I know I only have one problem I can't figure out why my char comparison values are always returning false even though I know that the characters are the same.
Here is the program code:
package ProgrammingExcercises.Ch09;
/**
*
* @author Justin VanDeBrake
*/
import java.util.Arrays;
import java.util.Scanner;
import java.io.File;
public class HangmanFile33 {
public static void main(String[] args) throws Exception {
File Easy = new File("9_33Easy.txt");
File Medium = new File("9_33Medium.txt");
File Hard = new File("9_33Hard.txt");
File Extreme = new File("9_33Extreme.txt");
Scanner input = new Scanner(System.in);
Scanner inputEa = new Scanner(Easy);
Scanner inputMe = new Scanner(Medium);
Scanner inputHa = new Scanner(Hard);
Scanner inputEx = new Scanner(Extreme);
String word = "";
boolean Incomplete = true;
int misses = 0;
System.out.println("Press 0 to exit \nor \nEnter a Difficulty, Easy(1) Medium(2) Hard(3) or Extreme(4): ");
while(word.equals("")){
int Diff = input.nextInt();
input.nextLine();
if(Diff == 0)
System.exit(0);
else if(Diff == 1)
word = PickWord(inputEa, Easy);
else if(Diff == 2)
word = PickWord(inputMe, Medium);
else if(Diff == 3)
word = PickWord(inputHa, Hard);
else if(Diff == 4)
word = PickWord(inputEx, Extreme);
else
System.out.println("Invalid Difficulity. \n Please try again.");
}
int wordLength = word.length();
System.out.println("Word length:" + wordLength);
char letterArray[] = new char[wordLength];
char inputLetterMatch[] = new char[wordLength];
for(int i = 0; i <= wordLength - 1; i++){
inputLetterMatch[i] = '-';
}
while(Incomplete = true) {
System.out.print("Hangman word: ");
for(int i = 0; i < (inputLetterMatch.length); i++){
System.out.print(inputLetterMatch[i]);
}
System.out.println();
System.out.print("Enter a letter: ");
String guessString = input.nextLine();
char guess = guessString.charAt(0);
for(int i = 0; i <= word.length() - 1; i++){
char check = letterArray[i];
if (guess == check){
inputLetterMatch[i] = guess;
}else{
misses++;
System.out.println("Number of misses:" + misses);
}
if(Arrays.equals(inputLetterMatch, letterArray))
Incomplete = false;
}
}
}
public static String PickWord(Scanner s, File f) throws Exception{
String word;
double randomDouble = Math.random() * (CountWords(f) - 1);
System.out.println("RandomeDouble is: " + randomDouble);
int randomInt = (int)randomDouble;
System.out.println("RandomInt is: " + randomInt);
for(int i = 0; i < randomInt; i++) {
s.next();
}
word = s.next();
System.out.println("Chosen Word is:" + word);
return word;
}
public static int CountWords(File f) throws Exception{
int wordCount = 0;
Scanner s = new Scanner(f);
while(s.hasNext()) {
s.next();
wordCount++;
}
System.out.println("WordCount is:" + wordCount);
return wordCount;
}
}
and here is a run of the program:
Press 0 to exit
or
Enter a Difficulty, Easy(1) Medium(2) Hard(3) or Extreme(4):
1
WordCount is:39
RandomeDouble is: 23.64226405458706
RandomInt is: 23
Chosen Word is:sneakers
Word length:8
Hangman word: --------
Enter a letter: s
Number of misses:1
Number of misses:2
Number of misses:3
Number of misses:4
Number of misses:5
Number of misses:6
Number of misses:7
Number of misses:8
Hangman word: --------
Enter a letter: n
Number of misses:9
Number of misses:10
Number of misses:11
Number of misses:12
Number of misses:13
Number of misses:14
Number of misses:15
Number of misses:16
Hangman word: --------
Enter a letter: e
Number of misses:17
Number of misses:18
Number of misses:19
Number of misses:20
Number of misses:21
Number of misses:22
Number of misses:23
Number of misses:24
Hangman word: --------
Enter a letter: a
Number of misses:25
Number of misses:26
Number of misses:27
Number of misses:28
Number of misses:29
Number of misses:30
Number of misses:31
Number of misses:32
Hangman word: --------
Enter a letter: k
Number of misses:33
Number of misses:34
Number of misses:35
Number of misses:36
Number of misses:37
Number of misses:38
Number of misses:39
Number of misses:40
Hangman word: --------
Enter a letter: e
Number of misses:41
Number of misses:42
Number of misses:43
Number of misses:44
Number of misses:45
Number of misses:46
Number of misses:47
Number of misses:48
Hangman word: --------
Enter a letter: r
Number of misses:49
Number of misses:50
Number of misses:51
Number of misses:52
Number of misses:53
Number of misses:54
Number of misses:55
Number of misses:56
Hangman word: --------
Enter a letter: s
Number of misses:57
Number of misses:58
Number of misses:59
Number of misses:60
Number of misses:61
Number of misses:62
Number of misses:63
Number of misses:64
Hangman word: --------
Enter a letter:
The dashes represent the letters in the word and they should fill in with letters as the letters are guessed right. But for some reason the character comparison is never evaluating to true so the char are never filled in.