Replacing String placeholders with different characters

Question!

I'm in an intro cse class using Java and I have a new homework where I create a Mad Lib game using File Processing.

I am wondering what the best approach is to replace a String placeholder with an adjective like "cool"

Here is a small portion of my code

 PrintStream fileOutput = new PrintStream(new File(fileName));
 Scanner fileScan = new Scanner(new File(fileName));
  while (fileScan.hasNextLine()) {
     String line = fileScan.nextLine();
     Scanner word = new Scanner(line);
     while (word.hasNext()) {
        String token = word.next();
        if (token.startsWith("<") && token.endsWith(">")) {
           token = token.replace("<", "");
           token = token.replace(">", "");
        }
        fileOutput.print(token + " ");
     }
  }

I currently got the '<' and '>' characters taken care of but I am unsure what the best approach is to replace the characters in between the two brackets. For example if I identify a token is a placeholder and is adjective I would prompt the user to "Type an adjective" and a noun "Type a noun" using the correct a/an structure. On past assignments I would get the right external correctness but my style is "bad" or "incorrect"



Answers

I'm not entirely sure if this is the answer you're looking for, but I'll give it a shot. It seems that you might be interested in a few things:

PrintWriter is a Java class which allows you to write to a file instead of scanning it like you do with Scanner. If you are meant to generate a mad libs response based on a template with tags for adjective, verb, etc. then what you probably want to do is scan the file and write contents to a new output as you parse them. To get user input, you can use a separate Scanner object which takes values from System.in. For example, you'll want to use something like:

PrintWriter writer = new PrintWriter(file);
Scanner scanner = new Scanner(file);
Scanner input = new Scanner(System.in);

(your previous code here)

if (token.startsWith("<") && token.endsWith(">")
{
    System.out.println("Type a " + token + ":";
    writer.write(input.next());
}
else
{
    writer.write(token);
}

This clearly isn't perfect but hopefully you get the idea. If no tags are detected while scanning each word, then write the word to the file. If there is a tag detected, let the user define what is written in its place. Carry on for the rest of the file.

Edit: I just went back and saw that you use PrintStream. That will do the same thing. The purpose of my answer is demonstration of concept mostly.



How about something like this:

Scanner input = new Scanner(System.in);
// ...
    while (word.hasNext()) {
        String token = word.next();
        if (token.startsWith("<") && token.endsWith(">")) {
            token = token.substring(1, token.length() - 1);
            String article = token.matches("(?i)[aeiou].*") ? "an" : "a";
            System.out.println("Type " + article + " " + token);
            token = input.next();
        }
        fileOutput.print(token + " ");
    }
By : shmosel


This video can help you solving your question :)
By: admin