Repository navigation
Expand file tree
/
Copy pathGoel_MadLibs.java
More file actions
69 lines (54 loc) · 2.5 KB
/
Copy pathGoel_MadLibs.java
File metadata and controls
69 lines (54 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.Scanner;
public class Goel_MadLibs {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Define the original story with placeholders
String story = "Once upon a time, <name> found a magical <noun>.\n"
+ "They decided to <verb> it, and it turned out to be very <adjective>.\n"
+ "In the end, they discovered that the magic lasted for <number> days!";
// Display the original story
System.out.println("Original Mad Lib:");
System.out.println(story);
System.out.println();
// Prompt the user for inputs
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.print("Enter a noun: ");
String noun = scanner.nextLine();
System.out.print("Enter a verb: ");
String verb = scanner.nextLine();
System.out.print("Enter an adjective: ");
String adjective = scanner.nextLine();
System.out.print("Enter a number: ");
String number = scanner.nextLine();
// Replace <name>
int nameSpot = story.indexOf("<name>");
String beforeName = story.substring(0, nameSpot);
String afterName = story.substring(nameSpot + "<name>".length());
story = beforeName + name + afterName;
// Replace <noun>
int nounSpot = story.indexOf("<noun>");
String beforeNoun = story.substring(0, nounSpot);
String afterNoun = story.substring(nounSpot + "<noun>".length());
story = beforeNoun + noun + afterNoun;
// Replace <verb>
int verbSpot = story.indexOf("<verb>");
String beforeVerb = story.substring(0, verbSpot);
String afterVerb = story.substring(verbSpot + "<verb>".length());
story = beforeVerb + verb + afterVerb;
// Replace <adjective>
int adjSpot = story.indexOf("<adjective>");
String beforeAdj = story.substring(0, adjSpot);
String afterAdj = story.substring(adjSpot + "<adjective>".length());
story = beforeAdj + adjective + afterAdj;
// Replace <number>
int numSpot = story.indexOf("<number>");
String beforeNum = story.substring(0, numSpot);
String afterNum = story.substring(numSpot + "<number>".length());
story = beforeNum + number + afterNum;
// Display the completed story
System.out.println("\nHere's your mad lib story:");
System.out.println(story);
scanner.close();
}
}