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