Hi! So I'm trying to figure this question out, but I'm not surewhat to write for the
Question:
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.
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();
}
}
Fundamentals of Cost Accounting
ISBN: 978-0077398194
3rd Edition
Authors: William Lanen, Shannon Anderson, Michael Maher