Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Need help with an output problem , java hangman

  1. #1
    Junior Member
    Join Date
    Dec 2020
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with an output problem , java hangman

    Hey guys I made a simple game where of hangman but I am running into an issue where an output of a sentence is inputted more than 2 if the word contains the same character. This is how the output looks like, I just want it to output the sentence once.

    Output :
    Correct letter, good job!

    Correct letter, good job!

    -------

    | | |

    | |

    | |

    | |

    -------

    package wordguesser;
     
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.Scanner;
     
    public class easyMode {
     
        // starts the easy mode //
        public static void startGame() throws FileNotFoundException {
            System.out.println("\n" + "Welcome to easy mode, good luck!");
     
            // Loads words from the txt file and stores it into an arraylist one by one
            File wordlist = new File("wordlist.txt");
            Scanner readWords = new Scanner(wordlist);
            ArrayList<String> words = new ArrayList<>();
            while (readWords.hasNextLine()) {
                words.add(readWords.nextLine());
     
            }
            // stores the the word needed to be guessed
            String secretWord = words.get((int) (Math.random() * words.size()));
     
            /* for easy mode the words need to be less than 6 charaters long, this loop
             will check the length and see if it is less than 6 characters 
             */
            if (secretWord.length() > 5) {
                do {
                    secretWord = words.get((int) (Math.random() * words.size()));
                } while (secretWord.length() > 5);
            }
     
            // Stores the characters of the secret word, used for checking against the user guess 
            char[] wordChar = secretWord.toCharArray();
     
            // Stores what the user guesses //
            char[] userGuess = new char[wordChar.length];
     
            //loops makes the userGuess array equal to # which is used for checking later on
            for (int i = 0; i < wordChar.length; i++) {
                userGuess[i] = '#';
            }
     
            boolean guessed = false; // Stores if the word is guessed or not 
            int lives = 6;  // Stores the maximum lives a user can have 
     
            Scanner userInput = new Scanner(System.in);
            Scanner userChoice = new Scanner(System.in);
     
            System.out.println("-----------------------------------");
            System.out.println("Your word length is " + secretWord.length());
            System.out.println("Remaining lives: " + lives);
            character.drawCharacter(lives); // draws the inital stage of the character
            System.out.println("-----------------------------------");
     
            // the game will keep running until guessed is false 
            while (guessed == false) {
                System.out.print("Enter your guess: ");
     
                // Stores the character the user gueses //
                String input = userInput.next();
                input = input.toLowerCase();
     
                /* used for checking valid input, cant be a number or two characters 
                 together such as ab 
                 */
                while (input.length() != 1 || Character.isDigit(input.charAt(0))) {
                    System.out.println("Wrong input, Try again!");
                    input = userInput.next();
                }
     
                // Used for checking if user's inputted character is in the word or not 
                boolean found = false;
     
                /* checks if the users inputted characters matches any of the
                 charaters in the secret word if yes then found equals to true.
                 */
                for (int i = 0; i < wordChar.length; i++) {
                    if (input.charAt(0) == wordChar[i]) {
                        userGuess[i] = wordChar[i];
                        System.out.println("\n" + "Correct letter, good job!");
                        found = true;
                    }
                }
                /* If users inputted character doesnt match any of the characters then
                 lives is reduced by 1
                 */
                if (!found) {
                    lives--;
                    System.out.println("\n" + "Wrong Letter, Try again!");
                }
                // prints the characters corresponding to how many lives the user has
                character.drawCharacter(lives);
     
                // stores if game is finished or not //
                boolean finished = true;
     
                for (int i = 0; i < userGuess.length; i++) {
                    /* if a character is not guessed yet it will equal to # and so it
                     prints _ to show it still hasnt been guessed  
                     */
                    if (userGuess[i] == '#') {
                        System.out.print(" _");
                        finished = false;
                        /*
                         If a character is guessed then it prints the character 
                         instead of _
                         */
                    } else {
                        System.out.print(" " + userGuess[i]);
                    }
                }
                System.out.println();
                System.out.println("Remaining lives: " + lives );
                /* game ends if user guesses the word correctly but user can choose 
                 to play again if they want to if not they go back to game menu
                 */
                if (finished == true) {
                    System.out.println("-----------------------------------");
                    System.out.println("The word was " + secretWord);
                    System.out.println("You guessed the word correctly!");
                    System.out.println("-----------------------------------");
                    System.out.print("Press Y if you would like to play again: ");
                    char ch = userChoice.next().charAt(0);
                    ch = Character.toUpperCase(ch);
                    if (ch == 'Y') {
                        guessed = true;
                        easyMode.startGame();
                    } else {
                        guessed = true;
                    }
     
                }
                /* game ends if your lives run out but user can choose to play 
                 again if they want to if not they go back to game menu
                 */
                if (lives <= 0) {
                    System.out.println();
                    System.out.println("-----------------------------------");
                    System.out.println("Oh no, your lives ran out");
                    System.out.println("The word was " + secretWord);
                    System.out.println("Better luck next time!");
                    System.out.println("-----------------------------------");
                    System.out.println();
                    System.out.print("Press Y if you would like to play again: ");
                    char ch = userChoice.next().charAt(0);
                    ch = Character.toUpperCase(ch);
                    if (ch == 'Y') {
                        guessed = true;
                        easyMode.startGame();
                    } else {
                        guessed = true;
                    }
     
                }
            }
        }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with an output problem , java hangman

    The code doesn't compile for testing because of a missing class: character

    Also please copy and paste the full contents of the console window from the test showing all the input and output for the program.

    What variable has a value that tells you if the message has been printed yet?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 31
    Last Post: October 9th, 2014, 04:48 PM
  2. [SOLVED] Java runtime get result output from prompt problem with a larger output in type
    By kingwang98 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 14th, 2014, 08:52 AM
  3. hangman problem java coding
    By angelshark in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 8th, 2013, 12:55 PM
  4. For Loop and String Problem - Console Hangman Game
    By ashboi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 29th, 2012, 12:15 AM
  5. Java Hangman!
    By JavaManNoob in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 28th, 2012, 11:17 PM