Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a java program that plays a game where a player is asked to fill in variou

ID: 3547317 • 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.


This is all the code that I have and it cant read other files when I try to ask for it to search for one. It should be able to read any file and substitute any words that has a placeholder like this <adjective>. If you have any different code that you made and works that would be fine as well. Please Help!!




/*

* 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

/*

* 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 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++;

}


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);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote