Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class MadLibs {
- public static void main(String[] args) throws FileNotFoundException{
- System.out.println("Welcome to the game of Mad Libs.");
- System.out.println("I will ask you to provide various words");
- System.out.println("and phrases to fill in a story.");
- System.out.println("The result will be written to an output file.");
- }
- public static void menu() throws FileNotFoundException{
- Scanner console = new Scanner(System.in);
- System.out.print("(C)reate mad-lib, (V)iew mad-lib, (Q)uit mad-lib");
- String choice = console.nextLine();
- if (choice.equalsIgnoreCase("c")){
- //Create a Madlib
- create(console);
- }
- else if (choice.equalsIgnoreCase("v")){
- //view a madlib
- view(console);
- }
- else if (choice.equalsIgnoreCase("q")){
- //quit
- }
- else{
- menu();
- }
- }
- public static void create(Scanner console) throws FileNotFoundException{
- File input = getFile(console);
- System.out.print("Output file name: ");
- PrintStream out = new PrintStream(new File(console.nextLine()));
- System.out.println();
- Scanner fileScanner = new Scanner(input);
- while (fileScanner.hasNextLine()){
- Scanner line = new Scanner(fileScanner.nextLine());
- while (line.hasNext()) {
- // Either prompt or write it to the output
- String token = line.next();
- //System.out.print(token);
- if (token.startsWith("<") && token.endsWith(">")) {
- System.out.print("Please type a");
- token = token.substring(1, token.length()-1).replace("-", " ");
- if (isVowel(token.charAt(0))){
- System.out.print("n");
- }
- System.out.print(" " + token + ": ");
- token = console.nextLine();
- }
- out.print(token + " ");
- }
- menu();
- }
- if (c == 'a' || c== 'A' || c== 'e' || c== 'E' || c== 'i' || c== 'I' || c== 'o' || c== 'O' || c== 'u' || c== 'U') {
- System.out.print("n");
- }
- }
- public static void view(Scanner console) throws FileNotFoundException{
- File inputFile = getFile(console);
- System.out.println();
- Scanner file = new Scanner(inputFile);
- while(file.hasNextLine()){
- System.out.println(file.nextLine());
- }
- menu();
- }
- public static File getFile(Scanner console){
- System.out.print("Input file name: ");
- String fileName = console.nextLine();
- File inputFile = new File(fileName);
- while (!inputFile.exists()){
- System.out.print("File not found. Try again: ");
- inputFile = new File(console.nextLine());
- }
- return inputFile;
- }
- } `import java.io.*;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement