Use Scanner and File to read input from a file. Use PrintStream and File to write output to a file. Write and call methods that accept parameters and return values to manage information flow and add structure to programs. Follow prescribed conventions for spacing, indentation, naming, and comments
- Use Scanner and File to read input from a file.
- Use PrintStream and File to write output to a file.
- Write and call methods that accept parameters and return values to manage information flow and add structure to programs.
- Follow prescribed conventions for spacing, indentation, naming, and comments.
Notice that if an input file is not found, either for creating a mad-lib or viewing an existing one, the user is re-prompted. No re-prompting occurs for the output file. If the output file does not already exist, it is created. If it does already exist, its contents are overwritten. (These are the default behaviors in Java.) You may assume that the output file is not the same file as the input file.
When you are viewing an existing mad lib story, you are simply reading and echoing its contents to the console. You do not need to do any kind of testing to make sure that the story came from a mad lib input file; just output the file's contents.
Your
Input Files
You will need to download the example input files from our web site and save them to the same folder as your program. Mad lib input files are mostly just plain text, but they may also contain placeholders. Placeholders are represented as input tokens that begin with < and end with >. Placeholders may also contain additional < and > characters in the text of the placeholder. For example, the file tarzan.txt used in the previous log contains:
Sample input file: 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 he gets angry, he beats on his chest and says,
" <funny-noise> !" This is his war cry. Tarzan always dresses in
<adjective> shorts made from the skin of a/an <noun>
and his best friend is a/an <adjective> chimpanzee named
Cheetah. He is supposed to be able to speak to elephants and
<plural-noun> . In the movies, Tarzan is played by <person's-name> .
You may assume that each word/token from the input file is separated by neighboring words by a single space. In other words, when you are writing the output, you may place a single space after each token to separate them. You do not need to worry about blank spaces at the end of lines of the output file. It's okay to place a space after each line's last token. Your output mad lib story must retain the original placement of the line breaks from the input story.
Placeholders
Your program should break the input into lines and then into tokens using Scanner objects so that you can look for all its placeholders. Normal, non-placeholder word tokens can be written directly to the output file as-is, but placeholder tokens should cause the user to be prompted using the text inside the placeholder, with the beginning < and ending > removed. The user's response to the prompt is written to the madlib output file, rather than the placeholder itself. You should accept whatever response the user gives, even a multi-word answer or a blank answer.
Sometimes a placeholder has multiple words in it, separated by a hyphen (-), such as <proper-noun>. As your program discovers a placeholder, it should convert any such hyphens into spaces. Any hyphens that appear outside of a placeholder, such as in the other text of the story, should be retained and not converted into spaces.
When prompting the user to fill in a placeholder, give a different prompt depending on whether the text inside the placeholder begins with a vowel (a, e, i, o, or u, case-insensitively). If so, prompt for a response using "an". If not, use "a".
Placeholder | Resulting Prompt |
---|---|
<noun> | Please type a noun: |
<adjective> | Please type an adjective: |
<plural-noun> | Please type a plural noun: |
<Emotional-Actor's-NAME> | Please type an Emotional Actor's NAME: |
This program requires you to process user input, which you must do using a Scanner. All console input should be read using the nextLinemethod in the Scanner class, not the next method.
All file input and output in your program should be handled with the File, Scanner, and PrintStream classes. Do not use other methods of file I/O.
Here is the detailed explanation of the program
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 6 images