Search code examples
javaintellij-ideareplaceio

How to replace text in notepad using Java?


I am creating the HANGMAN game in which I have a .txt file called Keyboard and its content is as follows:-

Q W E R T Y U I O P
 A S D F G H J K L
  Z X C V B N M

And to print this, I use the regular way to do it:-

Scanner textScan = new Scanner(new File("src/Main/Keyboard"));
while(textScan.hasNextLine()) System.out.println(textScan.nextLine());

Now whenever the user enters a letter, like 'A' for example, I want to edit the notepad so that it becomes like:-

 Q W E R T Y U I O P
 - S D F G H J K L
  Z X C V B N M

So on and so forth. How do I edit this notepad and also reset it after one round(i.e. without any "-").

I use IntelliJ IDEA 2018.1.6 and I want to know how to clear the output window. In BlueJ,

System.out.print("\f");

Used to do the trick. But here, an arrow comes every time it goes to this line.


Solution

  • It appears that we agree in the comment section that the file should be scanned to a string for the sake of simplicity. The 2nd part can be solved with recursion and simple String methods. The following program does contain everything you wanted, but is not a fully functional game of hangman so I will let you fill in the blanks.

    import java.util.Scanner;
    import java.io.File;
    import java.io.FileNotFoundException;
    
    public class HangmanDriver {
    
    public static final int mistakes_MAX = 6;
    
    public static void main(String[] args) throws FileNotFoundException {
        File myFile = new File("PATH TO YOUR KEYBOARD FILE");
        String myText = "";
        Scanner in = new Scanner(myFile);
        while(in.hasNextLine()) {
            myText += (in.nextLine() + "\n");
        }
        in.close();
    
        String myWord = "";
        boolean stillPlaying = true;
    
        while(stillPlaying) {
            in = new Scanner(System.in);
            System.out.println("Enter the word for new game: ");
            myWord = in.nextLine().toUpperCase();
            playGame(in, false, 0, 0, myWord, myText);
            System.out.println("Would you like to play again? [Y/N]");
            char ans = in.next().charAt(0);
            if(ans != 'y' && ans != 'Y')
                stillPlaying = false;
        }
    
        System.out.println("Goodbye.");
    
    }
    
    public static void playGame(Scanner in, boolean isOver, int mistakes, int correct, String word, String text) {
        if(isOver == false) {   
            System.out.println("\n" + text + "\n\nEnter a Letter (Must Be Upper Case)");
            char letter = in.next().charAt(0);      
            if(text.indexOf(letter) != -1) {
                text = text.replace(letter, '-');
                if(word.indexOf(letter) != -1) {
                    for(char c : word.toCharArray()) {
                        if(letter == c)
                            correct++;
                    }
                    System.out.println("Good guess!");
                    if(correct == word.length()) {
                        System.out.println("You won!");
                        playGame(in, true, mistakes, correct, word, text);
                    } else {
                        playGame(in, false, mistakes, correct, word, text);
                    }
                } else {
                    mistakes++;
                    System.out.println(mistakes_MAX-mistakes + " guesses left.");
                    if(mistakes == mistakes_MAX) {
                        System.out.println("Game Over!");
                        playGame(in, true, mistakes, correct, word, text);
                    } else {
                        playGame(in, false, mistakes, correct, word, text);
                    }
                }
            } else {
                System.out.println("That is not an option. Try Again.\n\n");
                playGame(in, false, mistakes, correct, word, text);
            }
        }
    }
    
    }