0

I was writing program Hangman and have problem with repeat method

        Set<Character> guessed = new HashSet<>();
        String word = "_".repeat(wordLength); // it show me mistake here
        

         while (word.contains("_") && guessed.size() != maxGuesses) {

            System.out.println("");
            System.out.print("Word:");
            for (int i = 0; i < word.length(); i++) {
                System.out.print(" " + word.charAt(i));
            }
            System.out.println("");
            System.out.println("You have " + (maxGuesses - guessed.size()) + " attempts");
            System.out.println("Number of possible words: " + wordsList.size());
  • That's not the actual message. Be accurate. – user207421 Oct 29 '20 at 01:20
  • 1
    I guess the obvious question is: are you using Java 11, or did you end up with an older version somehow? – Silvio Mayolo Oct 29 '20 at 01:22
  • Mariana, to be clear, @Progman has the keys. You probably got the idea of the existence of that `repeat` method based on documentation for Java 11. It was added in that version. As @Progman's provided URL shows, there are more cumbersome ways of getting the same result with Java prior to v11. You must be using an earlier version of Java than 11 (or possibly just have your IDE set to a lower version), or that line would not lead to a compilation error. – CryptoFool Oct 29 '20 at 01:23
  • ...so you have two choices...1) upgrade to Java 11 or later syntax, or 2) implement that behavior a different way (see @Progman's URL). – CryptoFool Oct 29 '20 at 01:28
  • I got it! Thank you! – Mariana Oct 29 '20 at 02:14

0 Answers0