-1

I'm making a hangman game and I wanted to know how do I make it so if the user inputs 4, it will show the rules text file(rules.txt)


import java.util.Scanner;
class Hangman
{
  public static void main(String[] args)
  {
    Scanner guess = new Scanner(System.in);
    System.out.println("Hello, and welcome to Hangman!");
    System.out.println("Choose (1)easy   (2)medium   (3)hard (4)rules: ");
    int diff = guess.nextInt(); 
    if(diff==1)
    {
      Play obj = new Play();
        obj.Start(diff);
    }
     if(diff==2)
     {
        Play obj = new Play();
        obj.Start(diff);
      }
      if(diff==3)
      {
        Play obj = new Play();
        obj.Start(3);
      }
      else{
          System.out.print("enter a valid number, please try again");
      }
  }
}

Chathuranga Chandrasekara
  • 20,548
  • 30
  • 97
  • 138
Ans Fu
  • 1

1 Answers1

0

The java.nio.file.Files.readString can read an entire file into memory which you can then print out:

if (diff == 4) {
    try {
        System.out.println(Files.readString(Paths.get("rules.txt")));
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}
akarnokd
  • 69,132
  • 14
  • 157
  • 192
  • [link](https://stackoverflow.com/a/72621481/19258563) i added what you said and got this `Hangman.java:33: error: cannot find symbol System.out.println(Files.readString(Paths.get("rules.txt"))); ^ symbol: method readString(Path) location: class Files 1 error` – Ans Fu Jun 14 '22 at 22:28
  • What Java version are you using? Did you organize your imports, i.e., `import java.nio.file.*;`? – akarnokd Jun 15 '22 at 05:15
  • i believe its java 8, and yes i imported – Ans Fu Jun 15 '22 at 15:05
  • If you can, please upgrade to Java 11 at least. – akarnokd Jun 15 '22 at 15:24