Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi! So I'm trying to figure this question out, but I'm not surewhat to write for the code. Thanks.(this is in java) We now have

Hi! So I'm trying to figure this question out, but I'm not surewhat to write for the code. Thanks.(this is in java)

We now have a fully functional Mad Lib generator! Nice work!

Now it?s time to make your own Mad Libs. Edit the madlib.txtfile to create your own Mad Lib templates. Then run your program togenerate new Mad Lib stories!

Have friends and family input the words for the Mad Lib and seewhat comes out.

Here's what there is so far.

image

All that code in the MadLib.java is here (from thebeginning):

import java.io.File;import java.io.FileOutputStream;import java.io.PrintWriter;import java.io.FileNotFoundException;import java.util.Scanner;

public class MadLib { private Scanner in = newScanner(System.in); public static final String TITLE = "Welcome tothe game of Mad Libs.You will help create a story by providingwords and phrases.";

public void run() { System.out.println(TITLE); while (true) { Stringcommand = getString("(C)reate, (V)iew, or (Q)uit: ") .toLowerCase();

if(command.equals("c")) { create(); } else if(command.equals("v")) { view(); } else if(command.equals("q")) { break; } else{ System.out.println("Invalidcommand"); } }

System.out.println("Goodbye :("); }

public void create() { String fileName =getString("Enter a File Name:").toLowerCase(); if(fileName.equals("simple")) {

Scannerfilereader = getFile("input/simple"); PrintWriter writer = getWriter("output/simple"); readWrite(filereader, writer);

}

else if(fileName.equals("dance")) { Scannerfilereader = getFile("input/dance"); PrintWriter writer = getWriter("output/dance"); readWrite(filereader, writer);

}

else if(fileName.equals("clothes")) { Scannerfilereader = getFile("input/clothes"); PrintWriter writer = getWriter("output/clothes"); readWrite(filereader, writer);

}

else if(fileName.equals("tarzan")) { Scannerfilereader = getFile("input/tarzan"); PrintWriter writer = getWriter("output/tarzan"); readWrite(filereader, writer);

} else System.out.println("Not a valid file name, Please tryagain"); }

public Scanner getFile(String files) {

File file = newFile(files);

try { return newScanner(file);

} catch(FileNotFoundException e) { System.out.println(e.getMessage()); System.exit(-1); } return null;

}

public PrintWriter getWriter(String files){

try { // secondparameter as true will open file for appending FileOutputStream file = new FileOutputStream(files,true); return newPrintWriter(file);

} catch(FileNotFoundException e) { System.out.println(e.getMessage()); System.exit(-1); }

return null; }

public void readWrite(Scanner filereader,PrintWriter writer) {

while(filereader.hasNextLine()) { Stringline = filereader.nextLine(); System.out.println("i hit this2");

while(true) { int startindex =line.indexOf("<"); System.out.println(startindex); if (startindex == -1) break; int endindex = line.indexOf(">"); String data = line.substring(startindex +1, endindex); System.out.println(data); String word = getString("Please type a " +data + " :"); String begIndex =line.substring(startindex, (endindex + 1));

line = line.replace(begIndex, word);

} writer.println(line); } System.out.println("Mad Libhas been created"); writer.close();

}

public void view() {

String outFiles =getString("Enter a File Name:").toLowerCase();

if(outFiles.equals("simple")) {

Scannerfilereader = getFile("output/simple"); while(filereader.hasNextLine()) { String line = filereader.nextLine(); System.out.println(line); }

filereader.close(); }

else if(outFiles.equals("clothes")) { Scannerfilereader = getFile("output/clothes"); while(filereader.hasNextLine()) { String line = filereader.nextLine(); System.out.println(line); }

filereader.close(); }

else if(outFiles.equals("tarzan")) { Scannerfilereader = getFile("output/tarzan"); while(filereader.hasNextLine()) { String line = filereader.nextLine(); System.out.println(line); }

filereader.close(); }

else if(outFiles.equals("dance")) { Scannerfilereader = getFile("output/dance"); while(filereader.hasNextLine()) { String line = filereader.nextLine(); System.out.println(line); }

filereader.close(); }

else { System.out.println("sorry incorrect file"); } }

public String getString(String question){ System.out.print(question); return in.nextLine(); }

public static void main(String[] args) {

MadLib MadLib = newMadLib(); MadLib.run();

}

}image

image

image

8 FILES 1 MadLib.java madlib.txt MadLibFileReader.java !!! < 1 import java.io.File; 2 import java.io.FileOutputStream; 3 import java.io.PrintWriter; 4 import java.io.FileNotFoundException; 5 import java.util.Scanner; 6 7. public class MadLib { 8 9 10 11- 12 13- 14 15 16 PEP 678 17- 18 19 20 21- 22 23 24 25 26 27 private Scanner in = new Scanner (System.in); public static final String TITLE= Welcome to the game of Mad Lib public void run() { System.out.println(TITLE); while (true) { } String command = getString( (C)reate, (V)iew, or (Q)uit: .toLowerCase(); if (command.equals( c )) { create(); } else if (command.equals( v )) { view(); } else if (command.equals( q )) { break; } else { } System.out.println( Invalid command ); 11 8 FILES D MadLib.java madlib.txt MadLibFileReader.java !!! 1 Write your own Mad Lib template here! 2 FILES 1 MadLib.java madlib.txt MadLibFileReader.java !!! < 1 import java.io.*; 2 import java.nio.charset. StandardCharsets; 3 import java.nio.file.Files; 4 import java.nio.file.Paths; 5 6 7 8 9 10 11 12- { 13 14 15 16 17 18 19 // This is the MadLibFileReader class // It reads in a madlib text file and converts it into a Java String // Feel free to explore this code, it uses the Java Files class // to read in a Mad Lib template from the madlib.txt file. // DO NOT CHANGE THIS CODE, the program may not work if you do. public class MadLibFileReader private static final String FILENAME private String template; public MadLibFileReader() { } 20 21 22 23 24 25 26 27 template = load Template (FILENAME); public String getMadLibTemplate() { } return template; madlib.txt ; private String load Template(String filename) FILES 0 MadLib.java madlib.txt MadLibFileReader.java 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40- 41 42 43 44 45 46 47 48} 49 } return template; private String load Template(String filename) { String result = // Try to run the following code, but there may be errors // when reading the file. try { } // Read every byte of data in the file, and convert all by // to a standard Unicode String byte[] encoded = Files.readAllBytes (Paths.get (filename)); result = new String(encoded, StandardCharsets. UTF_8); } // If there was an error reading the file, this catch clause w catch (IOException e) { System.out.println( There was an error while reading the f e.printStackTrace(); } // Return the result return result;

Step by Step Solution

3.40 Rating (153 Votes )

There are 3 Steps involved in it

Step: 1

It looks like youve provided the code for a Java program that creates Mad Libs using ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals of Cost Accounting

Authors: William Lanen, Shannon Anderson, Michael Maher

3rd Edition

9780078025525, 9780077517359, 77517350, 978-0077398194

More Books

Students also viewed these Programming questions