Write a program that plays a game where a player is asked to fill in various words of a mostly complete story without being able to see the rest. Then the user is shown his/her story, which is often funny. The input for your program is a set of story files, each of which contains “placeholder” tokens surrounded by < and >, such as:
One of the most <adjective> characters in fiction is named "Tarzan of the <plural-noun> ." Tarzan was raised by a/an <noun> and lives in the <adjective> jungle in the heart of darkest <place> .
The user is prompted to fill in each of the placeholders in the
story, and then a resulting output file is created with the
placeholders filled in. For example:
Input file name? story1.txt
Please enter an adjective: silly
Please enter a plural noun: socks
Please enter a noun: tree
Please enter an adjective: tiny
Please enter a place: Canada
The resulting output story would be:
One of the most silly characters in fiction
is named "Tarzan of the socks ." Tarzan was raised by a/an tree and
lives in the tiny jungle in the heart of darkest Canada
.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// Story.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class Story {
/**
* method to open a fil, read all text and return the text
*
* @param filename
* name of file
* @return file contents
* @throws FileNotFoundException
* if file is not found
*/
static String openFile(String filename) throws FileNotFoundException {
Scanner scanner = new Scanner(new File(filename));
String text = "";
// appending all lines to the variable text
while (scanner.hasNext()) {
text += scanner.nextLine();
if (scanner.hasNext()) {
text += "\n";
}
}
scanner.close();
return text;
}
/**
* method to extract placeholders from a file and return an array list
* containing all the found placeholders
*
* @param input
* input text
* @return list containing all placeholders
*/
static ArrayList<String> extractPlaceHolders(String input) {
// creating an array list
ArrayList<String> placeholders = new ArrayList<String>();
// looping as long as there is a pair of < and > is present
while (input.contains("<") && input.contains(">")) {
// finding indices of < and >
int index1 = input.indexOf("<");
int index2 = input.indexOf(">");
// extracting the string between index1 and index2
String placeHolder = input.substring(index1 + 1, index2);
// adding to the list
placeholders.add(placeHolder);
// updating text to find the next pair of < >
input = input.substring(index2 + 1);
}
return placeholders;
}
/**
* method to save some text to an output file
*/
static void saveFile(String data, String filename)
throws FileNotFoundException {
PrintWriter writer = new PrintWriter(new File(filename));
writer.print(data);
writer.close();
System.out.println("Output has been saved to " + filename);
}
public static void main(String[] args) {
// scanner to read input
Scanner scanner = new Scanner(System.in);
System.out.print("Input file name? ");
// getting filename
String filename = scanner.nextLine();
try {
// opening file
String text = openFile(filename);
// extracting placeholders
ArrayList<String> placeHolders = extractPlaceHolders(text);
// looping through each placeholder
for (String str : placeHolders) {
// asking to enter a replacement
System.out.print("Please enter a/an " + str + ": ");
String replacement = scanner.nextLine();
// replacing current placeholder with input
text = text.replaceFirst("<" + str + ">", replacement);
}
// displaying output, remove below two lines if you do not want to
// display the output
System.out.println("Output:");
System.out.println(text);
// saving output to a file named output.txt
saveFile(text, "output.txt");
} catch (FileNotFoundException e) {
System.err.println(e.getMessage());
}
}
}
/*OUTPUT*/
Input file name? story.txt
Please enter a/an adjective: dumb
Please enter a/an plural-noun: socks
Please enter a/an noun: fire
Please enter a/an adjective: big
Please enter a/an place: Moon
Output:
One of the most dumb characters in fiction is named "Tarzan of the socks ." Tarzan was raised by a/an fire and lives in the big jungle in the heart of darkest Moon .
Output has been saved to output.txt
Write a program that plays a game where a player is asked to fill in various...
JAVA QUESTION: JAVA QUESTION instructions: make something similar to the example output that is given below + JAVA DOC Madlibs Lab: Here are the input files examples Tarzan txt : One of the most <adjective> characters in fiction is named "Tarzan of the <plural-noun> ." Tarzan was raised by a/an <noun> and lives in the <adjective> jungle in the heart of darkest <place> . He spends most of his time eating <plural-noun> and swinging from tree to <noun> . Whenever...