My Hangman code doesn't log incorrect errors right away, prints double sets of underscores and more

I have three main problems with my Hangman game replication code:

The β€œfill” part is printed twice twice, correct guesses are randomly registered as incorrect
enter image description here

and incorrect guesses are not always taken into account.
enter image description here This is the code for the main class:

    public class Main 
    {

        public static void main(String[] args) 
        {
            Hangman manHang = new Hangman();    
        }

    }

The Hangman class:

    import java.util.Random;


    public class Hangman
            {
                Random r;
                GetData get;
                String[] Bank = {"consider","minute","accord","evident","practice","intend","concern","commit","issue","approach","establish","utter","conduct","engage","obtain","scarce","policy","straight","stock","apparent","property","fancy","concept","court","appoint","ambiguous","arbitrary","alliteration","arrogant","benevolent","belligerent","boycott","cynical","connotation","cessation","contemporary","craving","grandiose","gratuitous","guile","harbinger","impetuous","incandescent","indigent","inexorable","injunction","insipid","insurgent","languish","magnate","abjure","abrogate","abstemious", "acumen", "antebellum","auspicious","belie","bellicose","bowdlerize","chicanery","chromosome","churlish","circumlocution","circumnavigate","deciduous","deleterious","diffident","enervate","enfranchise","epiphany","equinox","evanescent","expurgate","facetious", "fallacious"};
                String word;//Stores the random word used
                boolean finished = false;
                int incGuessCount = 0;
                boolean[] lettersFound;//Used to mark which letters have been found
                String guessedLetter=" ";//Used to store guesses
                boolean playerHasWon = false;

            public Hangman()
            {
                r = new Random();
                get = new GetData();
                word=Bank[r.nextInt(Bank.length)]; //Selects a random word and assigns word the value
                lettersFound = new boolean[word.length()]; //Creates a boolean array the length of the word
                int incGuessCount = 0;

                while(incGuessCount<5 && playerHasWon == false)
                {
                    drawGallows(); //Show the Gallows depending on how many incorrect guesses there are
                    displayWord();
                    getGuess();
                    checkGuess();
                    //checkVictory();

                }

                if (incGuessCount>=5)
                {
                    System.out.print('\u000C');//Clears the screen
                    fiveWrong();//Displays full Hangman
                    System.out.println("You have lost! The word was "+word);
                }
            }

        public void getGuess()
        {
            System.out.println("\u000C");
            System.out.println(" ");
            System.out.println("What letter do you guess?");
            System.out.println("You have "+(5-incGuessCount)+" guesses left.");
            System.out.print("Enter guess:");
            guessedLetter = get.aWord();//Uses scanners to take in the guesses
        }


        public boolean displayWord()            
        {
            boolean goodGuess = false;//Assumes guess is bad automatically
            char letter = guessedLetter.charAt(0);

            for(int i = 0;i<word.length();i++)//Goes through all the letters to check guess status
                if (lettersFound[i]==true)//Checks if a letter was already revealed at that position
                {
                 System.out.print(word.charAt(i)+" ");
                }

                    else if (word.charAt(i)==letter)//Prints the correctly guessed letter at the position
                    {
                        System.out.print(word.charAt(i)+" ");
                        lettersFound[i] = true;
                        goodGuess = true;
                    }
                        else//Fills in non-applicable spaces with an underscore
                            System.out.print("_ ");


            return goodGuess;           
        }   

        public void checkGuess()
            {
                 if(!displayWord() && incGuessCount==5)
                     fiveWrong();
                 else if(!displayWord() && incGuessCount<5)
                    {
                     incGuessCount++;
                     drawGallows();
                     getGuess();
                     displayWord();
                    }

                 else
                 {
                     drawGallows();
                     getGuess();
                     displayWord();
                 }
            }   

        public void defaultMan()
        {
            System.out.println('\u000C');
            System.out.println(" ________");
            System.out.println(" |      | ");
            System.out.println(" |      | ");                        
            System.out.println(" |      | ");                 
            System.out.println(" |       ");                   
            System.out.println(" |_________ ");
            System.out.println(" ");
        }

        public void oneWrong()
        {
            System.out.println('\u000C');
            System.out.println(" ________");
            System.out.println(" |      | ");
            System.out.println(" |      | ");                        
            System.out.println(" |      | ");                 
            System.out.println(" |       \\ ");                   
            System.out.println(" |_________ ");
            System.out.println(" ");
        }

        public void twoWrong()
        {
            System.out.println('\u000C');
            System.out.println(" ________");
            System.out.println(" |      | ");
            System.out.println(" |      | ");                        
            System.out.println(" |      | ");                 
            System.out.println(" |     / \\ ");                   
            System.out.println(" |_________ ");
            System.out.println(" ");
        }

        public void threeWrong()
        {
            System.out.println('\u000C');
            System.out.println(" ________");
            System.out.println(" |      | ");
            System.out.println(" |      | ");                        
            System.out.println(" |     /| ");                 
            System.out.println(" |     / \\ ");                   
            System.out.println(" |_________ "); 
            System.out.println(" ");
        }

        public void fourWrong()
        {
            System.out.println('\u000C');
            System.out.println(" ________");
            System.out.println(" |      | ");
            System.out.println(" |      | ");                        
            System.out.println(" |     /|\\ ");                 
            System.out.println(" |     / \\ ");                   
            System.out.println(" |_________ ");
            System.out.println(" ");
        }

        public void fiveWrong()
        {
            System.out.println('\u000C');
            System.out.println(" ________");
            System.out.println(" |      | ");
            System.out.println(" |     ( ) ");                        
            System.out.println(" |     /|\\ ");                 
            System.out.println(" |     / \\ ");                   
            System.out.println(" |_________ ");
            System.out.println(" ");
            System.out.println("You have lost! The word was "+word+".");
            System.out.println("Rerun the program to try again.");
            finished=true;
        }

        public void drawGallows()
        {
            if(incGuessCount==0)
            {
                defaultMan();                   
            }

            if(incGuessCount==1)
            {
                oneWrong();                   
            }

            if(incGuessCount==2)
            {
                twoWrong();                  
            }

            if(incGuessCount==3)
            {
                 threeWrong();                 
            }

            if(incGuessCount==4)
            {
                fourWrong();                   
            }

            if(incGuessCount==5)
            {
                fiveWrong();

            }

            }
        } 

And the GetData class:

    import java.util.Scanner;

    public class GetData
    { 
      private Scanner input; 

      public GetData()//Produces a scanner to take in input
      { input = new Scanner(System.in); } 

      public String aWord()//Gets the input as a guess/string
      { return input.next(); } 

      public int aNumber()//Gets the input as a number
      { return input.nextInt(); }

    }

Apologies for the poor organization, I am new to coding. I have worked through my code for hours with the help of several people, but we just cannot figure out where my logic is failing, and this is obvious in at least a few areas. Any help is appreciated, even if it simply points to more minor erroneous logic or useless variables. Thank.

: , , . . gameOver, true, , , . if:

if(corLetters == word.length())
                    gameOver = true;

            if (incGuessCount<5 && gameOver)
            {
                System.out.println("\u000c");
                System.out.println("Congratulations!");
                System.out.println("You have won!");
                System.out.println("Rerun the program to try again.");
            }

. , , - , :)

Hangman :

import java.util.Random;


public class Hangman
        {
            Random r;
            GetData get;
            String[] Bank = {"consider","minute","accord","evident","practice","intend","concern","commit","issue","approach","establish","utter","conduct","engage","obtain","scarce","policy","straight","stock","apparent","property","fancy","concept","court","appoint","ambiguous","arbitrary","alliteration","arrogant","benevolent","belligerent","boycott","cynical","connotation","cessation","contemporary","craving","grandiose","gratuitous","guile","harbinger","impetuous","incandescent","indigent","inexorable","injunction","insipid","insurgent","languish","magnate","abjure","abrogate","abstemious", "acumen", "antebellum","auspicious","belie","bellicose","bowdlerize","chicanery","chromosome","churlish","circumlocution","circumnavigate","deciduous","deleterious","diffident","enervate","enfranchise","epiphany","equinox","evanescent","expurgate","facetious", "fallacious"};
            String word;//Stores the random word used
            boolean finished = false;
            int incGuessCount = 0;
            int corLetters = 0;
            boolean[] lettersFound;//Used to mark which letters have been found
            String guessedLetter=" ";//Used to store guesses
            boolean gameOver = false;


            public Hangman()
            {
                r = new Random();
                get = new GetData();
                word=Bank[r.nextInt(Bank.length)]; //Selects a random word and assigns word the value
                lettersFound = new boolean[word.length()]; //Creates a boolean array the length of the word


                do
                {
                    drawGallows(); //Show the Gallows depending on how many incorrect guesses there are
                    displayWord();
                    getGuess();
                    checkGuess();

                }
                while(incGuessCount<5 && gameOver == false);

                if (incGuessCount>=5)
                {

                    fiveWrong();//Displays full Hangman

                }

                if(corLetters == word.length())
                    gameOver = true;

                if (incGuessCount<5 && gameOver)
                {
                    System.out.println("\u000c");
                    System.out.println("Congratulations!");
                    System.out.println("You have won!");
                    System.out.println("Rerun the program to try again.");
                }


            }

        public void getGuess()
        {
            System.out.println("\u000C");
            System.out.println(" ");
            System.out.println("What letter do you guess?");
            System.out.println("You have "+(5-incGuessCount)+" guesses left.");
            System.out.print("Enter guess:");
            guessedLetter = get.aWord();//Uses scanners to take in the guesses
        }


        public boolean displayWord()            
        {
            boolean goodGuess = false;//Assumes guess is bad automatically
            char letter = guessedLetter.charAt(0);

            for(int i = 0;i<word.length();i++)//Goes through all the letters to check guess status
                if (lettersFound[i]==true)//Checks if a letter was already revealed at that position
                {
                 System.out.print(word.charAt(i)+" ");
                 corLetters++;
                }

                    else if (word.charAt(i)==letter)//Prints the correctly guessed letter at the position
                    {
                        System.out.print(word.charAt(i)+" ");
                        lettersFound[i] = true;
                        goodGuess = true;
                        corLetters++;
                    }
                        else//Fills in non-applicable spaces with an underscore
                            System.out.print("_ ");


            return goodGuess;           
        }   

        public void checkGuess() {
             boolean disW = displayWord();

             if (!disW && incGuessCount == 5)
              fiveWrong();
             else if (!disW && incGuessCount < 5) {
              incGuessCount++;

             }
            }


        public void defaultMan()
        {
            System.out.println('\u000C');
            System.out.println(" ________");
            System.out.println(" |      | ");
            System.out.println(" |      | ");                        
            System.out.println(" |      | ");                 
            System.out.println(" |       ");                   
            System.out.println(" |_________ ");
            System.out.println(" ");
        }

        public void oneWrong()
        {
            System.out.println('\u000C');
            System.out.println(" ________");
            System.out.println(" |      | ");
            System.out.println(" |      | ");                        
            System.out.println(" |      | ");                 
            System.out.println(" |       \\ ");                   
            System.out.println(" |_________ ");
            System.out.println(" ");
        }

        public void twoWrong()
        {
            System.out.println('\u000C');
            System.out.println(" ________");
            System.out.println(" |      | ");
            System.out.println(" |      | ");                        
            System.out.println(" |      | ");                 
            System.out.println(" |     / \\ ");                   
            System.out.println(" |_________ ");
            System.out.println(" ");
        }

        public void threeWrong()
        {
            System.out.println('\u000C');
            System.out.println(" ________");
            System.out.println(" |      | ");
            System.out.println(" |      | ");                        
            System.out.println(" |     /| ");                 
            System.out.println(" |     / \\ ");                   
            System.out.println(" |_________ "); 
            System.out.println(" ");
        }

        public void fourWrong()
        {
            System.out.println('\u000C');
            System.out.println(" ________");
            System.out.println(" |      | ");
            System.out.println(" |      | ");                        
            System.out.println(" |     /|\\ ");                 
            System.out.println(" |     / \\ ");                   
            System.out.println(" |_________ ");
            System.out.println(" ");
        }

        public void fiveWrong()
        {
            System.out.println('\u000C');
            System.out.println(" ________");
            System.out.println(" |      | ");
            System.out.println(" |     ( ) ");                        
            System.out.println(" |     /|\\ ");                 
            System.out.println(" |     / \\ ");                   
            System.out.println(" |_________ ");
            System.out.println(" ");
            System.out.println("You have lost! The word was "+word+".");
            System.out.println("Rerun the program to try again.");
            gameOver=true;
        }

        public void drawGallows()
        {
            if(incGuessCount==0)
            {
                defaultMan();                   
            }

            if(incGuessCount==1)
            {
                oneWrong();                   
            }

            if(incGuessCount==2)
            {
                twoWrong();                  
            }

            if(incGuessCount==3)
            {
                 threeWrong();                 
            }

            if(incGuessCount==4)
            {
                fourWrong();                   
            }

            if(incGuessCount==5)
            {
                fiveWrong();

            }

            }
        } 
+4
2

, "" , , . , checkGuess():

if(!displayWord() && incGuessCount==5)
             fiveWrong();
else if(!displayWord() && incGuessCount<5)

displayWord() ( true, ) if. , checkGuess(). , checkWord(), :

public boolean checkWord()            
{
    boolean goodGuess = false;//Assumes guess is bad automatically
    char letter = guessedLetter.charAt(0);
    for(int i = 0;i<word.length();i++){//Goes through all the letters to check guess status
        if (word.charAt(i)==letter)
        {
            lettersFound[i] = true;
            goodGuess = true;
        }
    }

    return goodGuess;           
} 

if.

, , , checkGuess().. , while{}. :

public void checkGuess()
{
    if(!checkWord() && incGuessCount==5)
        fiveWrong();
    else if(!checkWord() && incGuessCount<5)
    {
        incGuessCount++;
    }
} 

, , ( ) . :

 r = new Random();
 get = new GetData();
 word=Bank[r.nextInt(Bank.length)];
 lettersFound = new boolean[word.length()];
 int incGuessCount = 0; // <<< This shouldn't be here

, , !

Edit:

, , . , displayWord() , , . :

if (lettersFound[i]==true)//Checks if a letter was already revealed at that position
{
    System.out.print(word.charAt(i)+" ");
    corLetters++;
}
else if (word.charAt(i)==letter)//Prints the correctly guessed letter at the position
{
    System.out.print(word.charAt(i)+" ");
    lettersFound[i] = true;
    goodGuess = true;
    corLetters++;
}

:

if (lettersFound[i]==true)//Checks if a letter was already revealed at that position
{
    System.out.print(word.charAt(i)+" ");
    //Deleted line here
}
else if (word.charAt(i)==letter)//Prints the correctly guessed letter at the position
{
    System.out.print(word.charAt(i)+" ");
    lettersFound[i] = true;
    goodGuess = true;
    corLetters++;
}

, , do while. , if checkGuess(). :

public void checkGuess() {
    boolean disW = displayWord();

    if (!disW && incGuessCount == 5)
        fiveWrong();
    else if (!disW && incGuessCount < 5) {
        incGuessCount++;

    }

    if (corLetters == word.length()){
        gameOver = true;
    }
} 
+1

.

: #checkGuess

:

if (!displayWord() && incGuessCount == 5)
  fiveWrong();

 else if (!displayWord() && incGuessCount < 5) {

? displayWord() , , ( ). ?

:

public void checkGuess() {
 boolean displayWordResult = displayWord(); //Rename this variable as you like

 if (!displayWordResult && incGuessCount == 5)
  fiveWrong();

 else if (!displayWordResult && incGuessCount < 5) {
  incGuessCount++;
  drawGallows();
  getGuess();
  displayWord();
 }

 else {
  drawGallows();
  getGuess();
  displayWord();
 }
}
  • :

#checkGuess. , :

else
{
    drawGallows();
    getGuess(); // What if I make wrong guess here? The "incGuessCount" won't increment and hence you wont see the change.
    displayWord();
}

, . . :

public void checkGuess() {
 boolean disW = displayWord();
 if (!disW && incGuessCount == 5)
  fiveWrong();
 else if (!disW && incGuessCount < 5) {
  incGuessCount++;
 }
}

, , , , , .

, . , .

: @StephenB , . , .

+1

All Articles