2

I created a PrintWriter and new text file and want to print my answers into the text file. However, because the code is embedded within the program's loop, every time the loop is restarted, a new text file is created to replace the last one. How do I write the code so that the text file isn't recreated every time the loop starts over?

Here is my code:

public class Wordler {

    public static void main(String[] args) throws IOException{

        //introduces the game
        System.out.println("Wordler: Finding words within a word");
        System.out.println(" ");
        System.out.println("Directions:");
        System.out.println("Create as many words as you can with the letters in the word given. Once you have written as many words as you can think of, type");
        System.out.println("'x' and then hit the enter key to end the game round. Good luck!");
        System.out.println("--------------------------------------------------------------------------------------------------------------------------");

        runGame();
    }

    //method to run the game
    public static void runGame() throws FileNotFoundException{

        //array list of arrays that contains all the possible words 
        ArrayList<String> wholeList = new ArrayList<String>();
        wholeList.add("vulnerability");
        wholeList.add("calculate");
        wholeList.add("virtual");

        //PrintWriter and File
        File results = new File("WordlerResults.txt");
        PrintWriter output = new PrintWriter(results);

        //list of words and their answers (as a sublist)
        ArrayList<String> arr1 = new ArrayList<String>();
        arr1.add("vulnerabiliy");
        arr1.add("ability");
        arr1.add("nearby");
        arr1.add("lite");
        arr1.add("near");
        arr1.add("bare");
        arr1.add("rule");
        arr1.add("bury");
        arr1.add("lair");
        arr1.add("rile");
        arr1.add("bear");
        arr1.add("liberality");
        arr1.add("virulently");
        arr1.add("vulnerably");
        arr1.add("inevitably");
        arr1.add("tenurially");
        arr1.add("inertially");
        arr1.add("neutrally");
        arr1.add("unlivable");
        arr1.add("unitarily");
        arr1.add("veniality");
        arr1.add("reliantly");
        arr1.add("brilliant");
        arr1.add("urinative");
        arr1.add("nailbiter");
        arr1.add("illuviate");
        arr1.add("unitively");
        arr1.add("veritably");
        arr1.add("trivially");
        arr1.add("vibratile");
        arr1.add("virtually");
        // stopped at #20, www.wordplays.com/w/13810606276/vulnerability

        List<String> arr1Sub = arr1.subList(1, 30); 

        ArrayList<String> arr2 = new ArrayList<String>();
        arr2.add("calculate");
        arr2.add("late");
        arr2.add("call");
        arr2.add("teal");
        arr2.add("talc");
        arr2.add("catcall");
        arr2.add("tall");
        arr2.add("cult");
        arr2.add("lace");
        arr2.add("tela");
        arr2.add("acute");
        arr2.add("lacteal");
        arr2.add("callet");
        arr2.add("acuate");
        arr2.add("luteal");
        arr2.add("actual");
        arr2.add("cullet");
        arr2.add("caecal");
        arr2.add("alulae");
        arr2.add("acetal");
        arr2.add("alate");
        arr2.add("caeca");
        arr2.add("aceta");
        arr2.add("eclat");
        arr2.add("cecal");
        arr2.add("lutea");
        arr2.add("cella");
        arr2.add("cleat");
        arr2.add("tulle");
        arr2.add("culet");
        arr2.add("alula");
        arr2.add("calla");
        arr2.add("tale");
        arr2.add("tace");
        arr2.add("celt");
        arr2.add("clue");
        arr2.add("alec");
        arr2.add("tell");
        arr2.add("cull");
        arr2.add("alae");
        arr2.add("cate");
        arr2.add("acta");
        arr2.add("tule");
        arr2.add("caca");
        arr2.add("ceca");
        arr2.add("tael");
        arr2.add("latu");
        arr2.add("lute");
        arr2.add("caul");
        arr2.add("cute");
        arr2.add("luce");
        arr2.add("cell");
        arr2.add("tala");

        List<String> arr2Sub = arr2.subList(1, 52);

        ArrayList<String> arr3 = new ArrayList<String>();
        arr3.add("virtual");
        arr3.add("ritual");
        arr3.add("vault");
        arr3.add("virtu");
        arr3.add("vital");
        arr3.add("trial");
        arr3.add("rival");
        arr3.add("viral");
        arr3.add("ultra");
        arr3.add("urial");
        arr3.add("trail");
        arr3.add("aril");
        arr3.add("vair");
        arr3.add("tali");
        arr3.add("virl");
        arr3.add("lair");
        arr3.add("rail");
        arr3.add("airt");
        arr3.add("vita");
        arr3.add("lati");
        arr3.add("vial");
        arr3.add("alit");
        arr3.add("tail");
        arr3.add("lair");
        arr3.add("rial");
        arr3.add("vatu");
        arr3.add("latu");
        arr3.add("tirl");
        arr3.add("ulva");
        arr3.add("litu");
        arr3.add("lira");
        arr3.add("lari");
        arr3.add("vail");

        List<String> arr3Sub = arr3.subList(1, 32);

        //input list
        ArrayList<String> inputList = new ArrayList<String>(); 
        Scanner input = new Scanner(System.in);

        //to print the words for the game
        int r = (int) (Math.random() * 2);
        String word = wholeList.get(r);
        System.out.println(word);

        while (input.hasNextLine()){
            String words = input.nextLine();
            if (words.equalsIgnoreCase("x")){
                break;
            }
            else{
                inputList.add(words);
            }
        }

        //check answers
        ArrayList<String> validAnswers = new ArrayList<String>();
        ArrayList<String> wrongAnswers = new ArrayList<String>();
        ArrayList<String> notFound = new ArrayList<String>();
        List<String> compare = new ArrayList<String>();

        if (r == 0){
            compare = arr1Sub;
        }
        else if (r == 1){
            compare = arr2Sub;
        }
        else if(r == 2){
            compare = arr3Sub;
        }
        else{
            compare.add("error");
            System.out.println(compare);
        }

        for (int i = 0; i < inputList.size(); i++){
            if (compare.contains(inputList.get(i))){ 
                validAnswers.add(inputList.get(i));
            }
            else if (!compare.contains(inputList.get(i))){
                wrongAnswers.add(inputList.get(i));
            }
            else{
                notFound.add(compare.get(i)); 
            }
        }

        System.out.println("Valid Answers: " + validAnswers);
        System.out.println("Wrong Answers: " + wrongAnswers);
        output.println(wholeList.get(r)); 
        output.println("Valid Answers: " + validAnswers);
        output.println("Wrong Answers: " + wrongAnswers);
        output.close();

        System.out.println(" ");
        System.out.println("Would you like to play again? (Y/N)");
        String response = input.nextLine();
        System.out.println(" ");

        if (response.equalsIgnoreCase("y")){
            repeatGame();
        }
        else if (response.equalsIgnoreCase("n")){
            System.out.println(" "); 
            System.out.println("Thank you for playing!");
        }
    }

    public static void repeatGame(){
        try {
            runGame();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


}
Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68

2 Answers2

2

Supposing that you want appending data by keeping the old written data in the file: Creating an instance of FileOutputStream(file, append) and wrapping it with PrintWriter should work:

  PrintWriter writer = new PrintWriter(new FileOutputStream(myFile, true));
       writer.write("a String");
       writer.close();
Sage
  • 15,290
  • 3
  • 33
  • 38
1

put the creating file codez into the main before you start the game the main would be like this

public static void main(String[] args) throws IOException{
...
File results = new File("WordlerResults.txt");
    PrintWriter output = new PrintWriter(results);
    runGame(output);
}

and the run game accept the output as input argument

public static void runGame(PrintWriter output) throws FileNotFoundException{
...
//also remove these two lines from here
//    File results = new File("WordlerResults.txt");
//    PrintWriter output = new PrintWriter(results);
}

but still the file is going to overridden for the next call(application run), so you just need to check the file exist state, and just markup the cursor to the end.

by the way don't forget to flush and close the output at the end of the programming life cycle as

output.flush();
output.close();