-1

I am making a adjective fill-in-the-blanks story creator in Java, as part of a student project. I have two .txt files: One file has the story itself, with the blanks written like this: __. I.E: "This is a __ story." The second file contains a list of adjectives.

I have gotten most of my code to work, but I am unable to read from the story-file, and replace the __ parts with random words from the adjectives-file. What is the best way to do this?

Let me know if you have any questions, thanks!

Dontosh
  • 31
  • 3
  • Do some more research. Try yourself. if you get stuck, specify the problem and post. also check [How to ask](https://stackoverflow.com/help/how-to-ask) – XO56 Feb 03 '21 at 19:48
  • Does this answer your question? [Replace text in files](https://stackoverflow.com/questions/3935791/find-and-replace-words-lines-in-a-file) You should read about replace() and replaceAll() methods from the String class. – Edgar Magallon Feb 03 '21 at 19:49

2 Answers2

1

Some good resources for reading files in java would be this website: https://www.geeksforgeeks.org/different-ways-reading-text-file-java/ There are many options avaliable and you can easily find what best fits your project there.

For replacing the blank spaces in the text file, it could be good to try with something like this:

//Creating a temporary copy of the file is useful in that case
FileWriter writer = new FileWriter(tempFile);

Reader reader = new FileReader(yourFile);
BufferedReader br = new BufferedReader(reader);

while(br.ready()) {
    writer.write(br.readLine().replaceAll("__", adjective));
}

writer.close();
br.close();
reader.close();

//And finally overwrite the initial file with the result
tempFile.renameTo(file);

I took the answer from this previously answered question : Files java replacing characters and adapted it to your case.

EmFa
  • 11
  • 2
  • Thanks for your reply, will definitely try to use FileReader instead of Scanner, and read up on how BufferedReader works! – Dontosh Feb 03 '21 at 20:57
0

My bad for not posting my code to begin with, this is what I have done by now. I am guessing that where I start to use a for-loop in my code is where it does not work. And I am not sure if I am using .equals the right way.

I will of course do some more research, but any pointers on my code below would be much appreciated!

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class StoryMaker {


    public void createStory() throws FileNotFoundException {

        ArrayList<String> storyArray = new ArrayList<>();
        ArrayList<String> adjectivesArray = new ArrayList<>();

        while (true) {

            System.out.println("----------------------------------");
            System.out.println("\t1. Lag adjektivhistorie automatisk" + "\n\t2. Lag adjektivhistorie med egne adjektiver" + "\n\t3. Avslutt");
            System.out.println("----------------------------------");

            Scanner scanner = new Scanner(System.in);
            int userInput = scanner.nextInt();
            switch (userInput) {
                case 1:
                    System.out.println("\t### Her er din automatisk lagde historie: ###");

                    File storyFile = new File("X:\\Users\\xx\\IdeaProjects\\Trinn6\\story.txt");
                    File adjectivesFile = new File("X:\\Users\\xx\\IdeaProjects\\Trinn6\\adjektiv.txt");
                    Scanner storyScanner = new Scanner(storyFile);
                    Scanner adjectivesScanner = new Scanner(adjectivesFile);
                    String adjectives = adjectivesScanner.nextLine();

                    while ( storyScanner.hasNextLine() ) {
                        storyArray.add(storyScanner.nextLine());
                    }
                    System.out.println(storyArray);

                    for (int i = 0; i < storyArray.size(); i++) {
                        if (storyArray.get(i).equals("__")) {
                            storyArray.replaceAll(+ adjectivesArray);
                            break;
                        }

                    }
            }
        }
    }
}
Dontosh
  • 31
  • 3