Write a java program that plays a game where a player is asked to fill in variou
ID: 3547305 • Letter: W
Question
Write a java 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 token surrounded by < and >. Your program must work properly given an arbitrary, properly formatted file. Below is an example of a story file:
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 (user input underlined):
Input file name? story1.txt <-- User Input
Output file name? out1.txt <-- User Input
Please enter an adjective: silly <-- User Input
Please enter a plural noun: socks <-- User Input
Please enter a noun: tree <-- User Input
Please enter an adjective: tiny <-- User Input
Please enter a place: Canada <-- User Input
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.
I cant read other files when I try to ask for it to search for one. Please Fix!
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package story;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;
/**
*
*
*/
public class Story {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(System.in);
Scanner inputFile = null;
String fileName;
String[] names = new String[6];
String[] questions = {"Please enter an adjective: ",
"Please enter a plural noun: ",
"Please enter a noun: ",
"Please enter an adjective: ",
"Please enter a place: "};
System.out.print("Input file name?: ");
fileName = input.nextLine();
try {
File file = new File(fileName);
inputFile = new Scanner(file);
} catch (Exception e) {
System.out.println(e.getMessage());
}
for (int i = 0; i < questions.length; i++) {
System.out.print(questions[i]);
names[i] = input.nextLine();
}
System.out.println();
setPlaceHolder(inputFile, names);
}
public static void setPlaceHolder(Scanner inputFile, String[] names) {
int i = 0;
int count = 0;
while (inputFile.hasNextLine()) {
String line = inputFile.nextLine();
String[] tokens = line.split(" ");
for (String s : tokens) {
if (!s.contains("<")) {
System.out.print(s + " ");
}
if (s.contains("<")) {
count++;
}
if (count == 2) {
i++;
}
if (s.contains("<")) {
System.out.print(names[i] + " ");
if (count == 2) {
count = 0;
}
}
}
count = 0;
System.out.println();
i++;
}
}
Explanation / Answer
I suppose you haven't taken both the input names of the file,
You took only one name of the file. And the check the syntax of scanner once
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.