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.

Recommended Answers

All 10 Replies

Is that line 79? If so, you need to exit the loop after finding a match - otherwize you just go on to test all the other chards - which don't match.

String s = "k" and char c = 'k' are different. you either need to tursn String s into a char or char c into a String before comparing. use proper methods for comparing though. hope that helps!

The chars should be filled in as soon as the if statement evaluates to true. The reason why the loop is not exited is that it is supposed to fill in all char values that are the same so it has to check the entire string before moving on. I only have the System.out.print(misses) statement in there to test whether it is working, which it does, but I never took it out. The problem is that the if statement is never evaluating to true.

I tried converting the string to char then comparing the two chars, comparing the string and char, and comparing two strings. I must be doing something wrong for none of those to work. I am using the string.equals method to check if two strings are equal and just the == operator to check if two chars are equal.

The dashes in the program output should be replaced with letters if the if statement was ever evaluating to true.

Where do you load the word into letterArray?

That was the problem thanks. I forgot to load it into the array now the code works for placing letters.

There is one last problem that I am having. the while loop is not exiting when the two arrays are equal.

incomplete = true;

        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;
                    correct++;
                }
                    
                if(correct == 0){
                    incorrect++;
                }

                if(Arrays.equals(inputLetterMatch, letterArray) == true){
                    incomplete = false;
                }
            }
        }

I just figured out how to make the loop exit. Make it to where it is testing whether or not the arrays are equal instead of testing whether or not Incomplete is true or false.

Here is my complete project.

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 =  "";
        int incorrect = 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] = '-';
        }

        letterArray = word.toCharArray();

        while(Arrays.equals(inputLetterMatch, letterArray) == false) {
            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);
            int correct = 0;
                
            for(int i = 0; i <= (word.length() - 1); i++){
                char check = letterArray[i];

                if (guess == check){
                    inputLetterMatch[i] = guess;
                    correct++;
                } 
            }
            if(correct == 0)
                incorrect++;
        }
        System.out.println("You have completed hangman your word was: " + word);
        if(incorrect > 1)
            System.out.println("You guessed wrong " + incorrect + " times.");
        else if(incorrect == 1)
            System.out.println("You guessed wrong " + incorrect + " time");
        else
            System.out.println("Perfect game!");
        System.out.println("Play again(yes(1) no(0))?");
        int playAgain = input.nextInt();
        if(playAgain == 1)
            HangmanFile33.main(args);
        else
            System.exit(0);
    }

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

Thanks for the help James.

Final code.

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 =  "";
        int incorrect = 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] = '-';
        }

        letterArray = word.toCharArray();

        while(Arrays.equals(inputLetterMatch, letterArray) == false) {
            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);
            int correct = 0;
                
            for(int i = 0; i <= (word.length() - 1); i++){
                char check = letterArray[i];

                if (guess == check){
                    inputLetterMatch[i] = guess;
                    correct++;
                } 
            }
            if(correct == 0)
                incorrect++;
        }
        System.out.println("You have completed hangman your word was: " + word);
        if(incorrect > 1)
            System.out.println("You guessed wrong " + incorrect + " times.");
        else if(incorrect == 1)
            System.out.println("You guessed wrong " + incorrect + " time");
        else
            System.out.println("Perfect game!");
        System.out.println("Play again(yes(1) no(0))?");
        int playAgain = input.nextInt();
        if(playAgain == 1)
            HangmanFile33.main(args);
        else
            System.exit(0);
    }

    public static String PickWord(Scanner s, File f) throws Exception{
        String word;
        double randomDouble = Math.random() * (CountWords(f) - 1);
        int randomInt = (int)randomDouble;
        for(int i = 0; i < randomInt; i++) {
            s.next();
        }
        word = s.next();
        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;
    }
}

There are a couple of errors that are not caught but as long as the program is run how it is intended they will not come up.

The errors have to do with there being no input.

That's great. Well done.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.