SHARE
TWEET

Untitled

a guest May 21st, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class MadLibs {
  5.    public static void main(String[] args)throws FileNotFoundException {
  6.       Scanner console = new Scanner(System.in);
  7.       boolean game = true;
  8.      
  9.       printIntro();
  10.      
  11.       while (game) {
  12.          game = options(console);
  13.       }
  14.    }
  15.    
  16.    public static void printIntro() {
  17.       System.out.println("Welcome to the game of Mad Libs.");
  18.       System.out.println("I will ask you to provide various words");
  19.       System.out.println("and phrases to fill in a story.");
  20.       System.out.println("The result will be written to an output file.");
  21.       System.out.println();
  22.    }
  23.    
  24.    public static boolean options(Scanner console) throws FileNotFoundException {
  25.       System.out.print("(C)reate mad-lib, (V)iew mad-lib, (Q)uit? ");
  26.       String choice = console.nextLine();
  27.      
  28.       if (choice.toLowerCase().equals("c")) {
  29.          createMadLib(console);
  30.          return true;
  31.       } else if (choice.toLowerCase().equals("v")) {
  32.          viewMadLib(console);
  33.          return true;
  34.       } else if (choice.toLowerCase().equals("q")) {
  35.          return false;
  36.       } else {
  37.          return true;
  38.       }
  39.    }
  40.    
  41.    public static void createMadLib(Scanner console) throws FileNotFoundException {
  42.       System.out.print("Input file name: ");
  43.       String inputFileName = console.nextLine();
  44.       File inFile = new File(inputFileName);
  45.      
  46.       while (!inFile.exists()) {
  47.          System.out.print("File not found. Try again: ");
  48.          inputFileName = console.nextLine();
  49.          inFile = new File(inputFileName);
  50.       }
  51.      
  52.       System.out.print("Output file name: ");
  53.       String outputFileName = console.nextLine();
  54.       File outFile = new File(outputFileName);
  55.       PrintStream output = new PrintStream(outFile);
  56.       System.out.println();
  57.       Scanner input = new Scanner(inFile);
  58.      
  59.       while (input.hasNextLine()) {
  60.          String line = input.nextLine();
  61.          Scanner lineScanner = new Scanner(line);
  62.          
  63.          while (lineScanner.hasNext()) {
  64.             String oldWord = lineScanner.next();
  65.          
  66.             if (oldWord.startsWith("<") && oldWord.endsWith(">")) {
  67.                char firstChar = oldWord.charAt(1);
  68.                oldWord = oldWord.replace("<", " ");
  69.                oldWord = oldWord.replace(">", "");
  70.                oldWord = oldWord.replace("-", " ");
  71.                String aOrAn = "a";
  72.                if (firstChar == 'a' || firstChar == 'e' || firstChar == 'i' || firstChar == 'o' ||
  73.                   firstChar == 'u' || firstChar == 'A' || firstChar == 'E' || firstChar == 'I' ||
  74.                   firstChar == 'O' || firstChar == 'U') {
  75.                   aOrAn = "an";
  76.                }
  77.                              
  78.                System.out.print("Please type " + aOrAn + oldWord + ": ");
  79.                String newWord = console.nextLine();
  80.                output.print(newWord + " ");
  81.             } else {
  82.                output.print(oldWord + " ");
  83.             }
  84.          }
  85.          output.println();
  86.       }
  87.      
  88.       System.out.println("Your mad-lib has been created!");
  89.       System.out.println();
  90.    }
  91.    
  92.    public static void viewMadLib(Scanner console) throws FileNotFoundException {
  93.       System.out.print("Input file name: ");
  94.       String fileToView = console.nextLine();
  95.       File fileView = new File(fileToView);
  96.  
  97.       while (!fileView.exists()) {
  98.          System.out.print("File not found. Try again: ");
  99.          fileToView = console.nextLine();
  100.          fileView = new File(fileToView);
  101.       }
  102.       System.out.println();
  103.       Scanner fileScanner = new Scanner(fileView);
  104.  
  105.       while (fileScanner.hasNextLine()) {
  106.          String text = fileScanner.nextLine();
  107.          System.out.println(text);
  108.       }
  109.       System.out.println();
  110.    }  
  111. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top