Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class MadLibs {
  5.  
  6. public static void main(String[] args) throws FileNotFoundException{
  7. System.out.println("Welcome to the game of Mad Libs.");
  8. System.out.println("I will ask you to provide various words");
  9. System.out.println("and phrases to fill in a story.");
  10. System.out.println("The result will be written to an output file.");
  11. }
  12.  
  13. public static void menu() throws FileNotFoundException{
  14. Scanner console = new Scanner(System.in);
  15. System.out.print("(C)reate mad-lib, (V)iew mad-lib, (Q)uit mad-lib");
  16. String choice = console.nextLine();
  17.  
  18. if (choice.equalsIgnoreCase("c")){
  19. //Create a Madlib
  20. create(console);
  21. }
  22. else if (choice.equalsIgnoreCase("v")){
  23. //view a madlib
  24. view(console);
  25. }
  26. else if (choice.equalsIgnoreCase("q")){
  27. //quit
  28. }
  29. else{
  30. menu();
  31. }
  32. }
  33. public static void create(Scanner console) throws FileNotFoundException{
  34. File input = getFile(console);
  35. System.out.print("Output file name: ");
  36. PrintStream out = new PrintStream(new File(console.nextLine()));
  37. System.out.println();
  38. Scanner fileScanner = new Scanner(input);
  39. while (fileScanner.hasNextLine()){
  40. Scanner line = new Scanner(fileScanner.nextLine());
  41. while (line.hasNext()) {
  42. // Either prompt or write it to the output
  43. String token = line.next();
  44. //System.out.print(token);
  45. if (token.startsWith("<") && token.endsWith(">")) {
  46. System.out.print("Please type a");
  47. token = token.substring(1, token.length()-1).replace("-", " ");
  48. if (isVowel(token.charAt(0))){
  49. System.out.print("n");
  50. }
  51. System.out.print(" " + token + ": ");
  52. token = console.nextLine();
  53. }
  54.  
  55. out.print(token + " ");
  56.  
  57. }
  58.  
  59. menu();
  60. }
  61.  
  62. if (c == 'a' || c== 'A' || c== 'e' || c== 'E' || c== 'i' || c== 'I' || c== 'o' || c== 'O' || c== 'u' || c== 'U') {
  63. System.out.print("n");
  64. }
  65. }
  66.  
  67. public static void view(Scanner console) throws FileNotFoundException{
  68. File inputFile = getFile(console);
  69. System.out.println();
  70.  
  71. Scanner file = new Scanner(inputFile);
  72. while(file.hasNextLine()){
  73. System.out.println(file.nextLine());
  74. }
  75.  
  76. menu();
  77. }
  78.  
  79.  
  80. public static File getFile(Scanner console){
  81. System.out.print("Input file name: ");
  82. String fileName = console.nextLine();
  83. File inputFile = new File(fileName);
  84. while (!inputFile.exists()){
  85. System.out.print("File not found. Try again: ");
  86. inputFile = new File(console.nextLine());
  87. }
  88. return inputFile;
  89. }
  90. } `import java.io.*;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement