MadLibs Java Project Issues

When I complete this mad libs project about a story, I am unsure what I am doing wrong as I thought I debugged everything. There seems to be a lot of errors, and I followed the directions correctly.

The code:
String story = "This morning “+name1+” woke up feeling “+adjective1+”. ‘It is going to be a “+adjective2+” day!’ Outside, a bunch of "+noun1+"s were protesting to keep “+noun2+” in stores. They began to “+verb1+” to the rhythm of the “+noun3+”, which made all the "+noun4+"s very “+adjective3+”. Concerned, “+name1+” texted “+name2+”, who flew “+name1+” to “+place1+” and dropped “+name1+” in a puddle of frozen “+noun5+”. “+name1+” woke up in the year “+number+”, in a world where "+noun6+“s ruled the world.”;

After naming and initializing the strings, the code seems to be wrong:

String name1 = “Bobby”;

  String name2 = "Jake";

  String adjective1 = "happy";

  String adjective2 = "exciting";

  String adjective3 = "upset";

  String verb1 = "chant";

  String noun1 = "farmers";

  String noun2 = "Milk";

  String noun3 = "We want milk back";

  String noun4 = "townspeople";

  String noun5 = "toxic sludge";

  String noun6 = "Cows";

  int number = 2120;

  String place1 = "Planet Anamorphis";

  

  System.out.println(story);

I would be grateful for any help. Thank you!

Hello @arc7697720166, welcome to the forums! What exactly isn’t working here? Can you post any error messages you get or unexpected outcomes, please?

Thank you for your reply. This are all the errors to the code above:

MadLibs.java:10: error: cannot find symbol
String story = "This morning “+name1+” woke up feeling “+adjective1+”. ‘It is going to be a “+adjective2+” day!’ Outside, a bunch of "+noun1+"s were protesting to keep “+noun2+” in stores. They began to “+verb1+” to the rhythm of the “+noun3+”, which made all the "+noun4+"s very “+adjective3+”. Concerned, “+name1+” texted “+name2+”, who flew “+name1+” to “+place1+” and dropped “+name1+” in a puddle of frozen “+noun5+”. “+name1+” woke up in the year “+number+”, in a world where "+noun6+“s ruled the world.”;
^
symbol: variable name1
location: class MadLibs
MadLibs.java:10: error: cannot find symbol
String story = "This morning “+name1+” woke up feeling “+adjective1+”. ‘It is going to be a “+adjective2+” day!’ Outside, a bunch of "+noun1+"s were protesting to keep “+noun2+” in stores. They began to “+verb1+” to the rhythm of the “+noun3+”, which made all the "+noun4+"s very “+adjective3+”. Concerned, “+name1+” texted “+name2+”, who flew “+name1+” to “+place1+” and dropped “+name1+” in a puddle of frozen “+noun5+”. “+name1+” woke up in the year “+number+”, in a world where "+noun6+“s ruled the world.”;
^
symbol: variable adjective1
location: class MadLibs
MadLibs.java:10: error: cannot find symbol
String story = "This morning “+name1+” woke up feeling “+adjective1+”. ‘It is going to be a “+adjective2+” day!’ Outside, a bunch of "+noun1+"s were protesting to keep “+noun2+” in stores. They began to “+verb1+” to the rhythm of the “+noun3+”, which made all the "+noun4+"s very “+adjective3+”. Concerned, “+name1+” texted “+name2+”, who flew “+name1+” to “+place1+” and dropped “+name1+” in a puddle of frozen “+noun5+”. “+name1+” woke up in the year “+number+”, in a world where "+noun6+“s ruled the world.”;
^
symbol: variable adjective2
location: class MadLibs
MadLibs.java:10: error: cannot find symbol
String story = "This morning “+name1+” woke up feeling “+adjective1+”. ‘It is going to be a “+adjective2+” day!’ Outside, a bunch of "+noun1+"s were protesting to keep “+noun2+” in stores. They began to “+verb1+” to the rhythm of the “+noun3+”, which made all the "+noun4+"s very “+adjective3+”. Concerned, “+name1+” texted “+name2+”, who flew “+name1+” to “+place1+” and dropped “+name1+” in a puddle of frozen “+noun5+”. “+name1+” woke up in the year “+number+”, in a world where "+noun6+“s ruled the world.”;
^
symbol: variable noun1
location: class MadLibs
MadLibs.java:10: error: cannot find symbol
String story = "This morning “+name1+” woke up feeling “+adjective1+”. ‘It is going to be a “+adjective2+” day!’ Outside, a bunch of "+noun1+"s were protesting to keep “+noun2+” in stores. They began to “+verb1+” to the rhythm of the “+noun3+”, which made all the "+noun4+"s very “+adjective3+”. Concerned, “+name1+” texted “+name2+”, who flew “+name1+” to “+place1+” and dropped “+name1+” in a puddle of frozen “+noun5+”. “+name1+” woke up in the year “+number+”, in a world where "+noun6+“s ruled the world.”;
^
symbol: variable noun2
location: class MadLibs
MadLibs.java:10: error: cannot find symbol
String story = "This morning “+name1+” woke up feeling “+adjective1+”. ‘It is going to be a “+adjective2+” day!’ Outside, a bunch of "+noun1+"s were protesting to keep “+noun2+” in stores. They began to “+verb1+” to the rhythm of the “+noun3+”, which made all the "+noun4+"s very “+adjective3+”. Concerned, “+name1+” texted “+name2+”, who flew “+name1+” to “+place1+” and dropped “+name1+” in a puddle of frozen “+noun5+”. “+name1+” woke up in the year “+number+”, in a world where "+noun6+“s ruled the world.”;
^
symbol: variable verb1
location: class MadLibs
MadLibs.java:10: error: cannot find symbol
String story = "This morning “+name1+” woke up feeling “+adjective1+”. ‘It is going to be a “+adjective2+” day!’ Outside, a bunch of "+noun1+"s were protesting to keep “+noun2+” in stores. They began to “+verb1+” to the rhythm of the “+noun3+”, which made all the "+noun4+"s very “+adjective3+”. Concerned, “+name1+” texted “+name2+”, who flew “+name1+” to “+place1+” and dropped “+name1+” in a puddle of frozen “+noun5+”. “+name1+” woke up in the year “+number+”, in a world where "+noun6+“s ruled the world.”;
^
symbol: variable noun3
location: class MadLibs
MadLibs.java:10: error: cannot find symbol
String story = "This morning “+name1+” woke up feeling “+adjective1+”. ‘It is going to be a “+adjective2+” day!’ Outside, a bunch of "+noun1+"s were protesting to keep “+noun2+” in stores. They began to “+verb1+” to the rhythm of the “+noun3+”, which made all the "+noun4+"s very “+adjective3+”. Concerned, “+name1+” texted “+name2+”, who flew “+name1+” to “+place1+” and dropped “+name1+” in a puddle of frozen “+noun5+”. “+name1+” woke up in the year “+number+”, in a world where "+noun6+“s ruled the world.”;
^
symbol: variable noun4
location: class MadLibs
MadLibs.java:10: error: cannot find symbol
String story = "This morning “+name1+” woke up feeling “+adjective1+”. ‘It is going to be a “+adjective2+” day!’ Outside, a bunch of "+noun1+"s were protesting to keep “+noun2+” in stores. They began to “+verb1+” to the rhythm of the “+noun3+”, which made all the "+noun4+"s very “+adjective3+”. Concerned, “+name1+” texted “+name2+”, who flew “+name1+” to “+place1+” and dropped “+name1+” in a puddle of frozen “+noun5+”. “+name1+” woke up in the year “+number+”, in a world where "+noun6+“s ruled the world.”;
^
symbol: variable adjective3
location: class MadLibs
MadLibs.java:10: error: cannot find symbol
String story = "This morning “+name1+” woke up feeling “+adjective1+”. ‘It is going to be a “+adjective2+” day!’ Outside, a bunch of "+noun1+"s were protesting to keep “+noun2+” in stores. They began to “+verb1+” to the rhythm of the “+noun3+”, which made all the "+noun4+"s very “+adjective3+”. Concerned, “+name1+” texted “+name2+”, who flew “+name1+” to “+place1+” and dropped “+name1+” in a puddle of frozen “+noun5+”. “+name1+” woke up in the year “+number+”, in a world where "+noun6+“s ruled the world.”;
^
symbol: variable name1
location: class MadLibs
MadLibs.java:10: error: cannot find symbol
String story = "This morning “+name1+” woke up feeling “+adjective1+”. ‘It is going to be a “+adjective2+” day!’ Outside, a bunch of "+noun1+"s were protesting to keep “+noun2+” in stores. They began to “+verb1+” to the rhythm of the “+noun3+”, which made all the "+noun4+"s very “+adjective3+”. Concerned, “+name1+” texted “+name2+”, who flew “+name1+” to “+place1+” and dropped “+name1+” in a puddle of frozen “+noun5+”. “+name1+” woke up in the year “+number+”, in a world where "+noun6+“s ruled the world.”;
^
symbol: variable name2
location: class MadLibs
MadLibs.java:10: error: cannot find symbol
String story = "This morning “+name1+” woke up feeling “+adjective1+”. ‘It is going to be a “+adjective2+” day!’ Outside, a bunch of "+noun1+"s were protesting to keep “+noun2+” in stores. They began to “+verb1+” to the rhythm of the “+noun3+”, which made all the "+noun4+"s very “+adjective3+”. Concerned, “+name1+” texted “+name2+”, who flew “+name1+” to “+place1+” and dropped “+name1+” in a puddle of frozen “+noun5+”. “+name1+” woke up in the year “+number+”, in a world where "+noun6+“s ruled the world.”;
^
symbol: variable name1
location: class MadLibs
MadLibs.java:10: error: cannot find symbol
String story = "This morning “+name1+” woke up feeling “+adjective1+”. ‘It is going to be a “+adjective2+” day!’ Outside, a bunch of "+noun1+"s were protesting to keep “+noun2+” in stores. They began to “+verb1+” to the rhythm of the “+noun3+”, which made all the "+noun4+"s very “+adjective3+”. Concerned, “+name1+” texted “+name2+”, who flew “+name1+” to “+place1+” and dropped “+name1+” in a puddle of frozen “+noun5+”. “+name1+” woke up in the year “+number+”, in a world where "+noun6+“s ruled the world.”;
^
symbol: variable place1
location: class MadLibs
MadLibs.java:10: error: cannot find symbol
String story = "This morning “+name1+” woke up feeling “+adjective1+”. ‘It is going to be a “+adjective2+” day!’ Outside, a bunch of "+noun1+"s were protesting to keep “+noun2+” in stores. They began to “+verb1+” to the rhythm of the “+noun3+”, which made all the "+noun4+"s very “+adjective3+”. Concerned, “+name1+” texted “+name2+”, who flew “+name1+” to “+place1+” and dropped “+name1+” in a puddle of frozen “+noun5+”. “+name1+” woke up in the year “+number+”, in a world where "+noun6+“s ruled the world.”;
^
symbol: variable name1
location: class MadLibs
MadLibs.java:10: error: cannot find symbol
String story = "This morning “+name1+” woke up feeling “+adjective1+”. ‘It is going to be a “+adjective2+” day!’ Outside, a bunch of "+noun1+"s were protesting to keep “+noun2+” in stores. They began to “+verb1+” to the rhythm of the “+noun3+”, which made all the "+noun4+"s very “+adjective3+”. Concerned, “+name1+” texted “+name2+”, who flew “+name1+” to “+place1+” and dropped “+name1+” in a puddle of frozen “+noun5+”. “+name1+” woke up in the year “+number+”, in a world where "+noun6+“s ruled the world.”;
^
symbol: variable noun5
location: class MadLibs
MadLibs.java:10: error: cannot find symbol
String story = "This morning “+name1+” woke up feeling “+adjective1+”. ‘It is going to be a “+adjective2+” day!’ Outside, a bunch of "+noun1+"s were protesting to keep “+noun2+” in stores. They began to “+verb1+” to the rhythm of the “+noun3+”, which made all the "+noun4+"s very “+adjective3+”. Concerned, “+name1+” texted “+name2+”, who flew “+name1+” to “+place1+” and dropped “+name1+” in a puddle of frozen “+noun5+”. “+name1+” woke up in the year “+number+”, in a world where "+noun6+“s ruled the world.”;
^
symbol: variable name1
location: class MadLibs
MadLibs.java:10: error: cannot find symbol
String story = "This morning “+name1+” woke up feeling “+adjective1+”. ‘It is going to be a “+adjective2+” day!’ Outside, a bunch of "+noun1+"s were protesting to keep “+noun2+” in stores. They began to “+verb1+” to the rhythm of the “+noun3+”, which made all the "+noun4+"s very “+adjective3+”. Concerned, “+name1+” texted “+name2+”, who flew “+name1+” to “+place1+” and dropped “+name1+” in a puddle of frozen “+noun5+”. “+name1+” woke up in the year “+number+”, in a world where "+noun6+“s ruled the world.”;
^
symbol: variable number
location: class MadLibs
MadLibs.java:10: error: cannot find symbol
String story = "This morning “+name1+” woke up feeling “+adjective1+”. ‘It is going to be a “+adjective2+” day!’ Outside, a bunch of "+noun1+"s were protesting to keep “+noun2+” in stores. They began to “+verb1+” to the rhythm of the “+noun3+”, which made all the "+noun4+"s very “+adjective3+”. Concerned, “+name1+” texted “+name2+”, who flew “+name1+” to “+place1+” and dropped “+name1+” in a puddle of frozen “+noun5+”. “+name1+” woke up in the year “+number+”, in a world where "+noun6+“s ruled the world.”;
^
symbol: variable noun6
location: class MadLibs
18 errors

Just curious, can you post your full code and format it correctly, please? There’s always a chance these errors are coming from the fact that I can’t see a class declaration, etc, but it most likely is something else…

I had the same problem, but I fixed it by putting the declared variables, before the String story variable just below the public static void name (String args). I don’t know what is that called, lol.

  String name1 = "  ";

  String adjective1 = "  ";

  String adjective2 = "  ";

  String adjective3 = "  ";

  String verb1 = "  ";

  String noun1 = "  ";

  String noun2 = "  ";

  String noun3 = "  ";

  String noun4 = "  ";

  String noun5 = "  ";

  String noun6 = "  ";

  String name2 = "  ";

  int number =  ;

  String place1 = "  ";
1 Like

This worked! Thank you so much for the solution!

1 Like

I had the same issue and this solved it. Thanks!

1 Like

Thanks! This worked.

1 Like

Same problem here. Thanks for the solution.

1 Like

While the solution has already been posted, I just want to point out that you generally wouldn’t define the story variable that way in real life because of the exact problem you ran into here. A simple way of bypassing the problem would be to declare the story variable without initializing it. Then make a function that either sets that string to the story variable or returns the string so you can set it manually in the main(). Or do it in a constructor.

String story;

void createStory() {
        this.story = "This morning "+name1+" woke up feeling "+adjective1+". ‘It is going to be a "+adjective2+" day!’ Outside, a bunch of "+noun1+"s were protesting to keep "+noun2+" in stores. They began to "+verb1+" to the rhythm of the "+noun3+", which made all the "+noun4+"s very "+adjective3+". Concerned, "+name1+" texted "+name2+", who flew "+name1+" to "+place1+" and dropped "+name1+" in a puddle of frozen "+noun5+". "+name1+" woke up in the year "+number+", in a world where "+noun6+"s ruled the world.";
    }

Or with a constructor. I generated this in about 2 seconds with IntelliJ’s auto generate tool.

public MadLib(String name1, String name2, String adjective1, String adjective2, String adjective3, String verb1, String noun1, String noun2, String noun3, String noun4, String noun5, String noun6, int number, String place1) {
        this.name1 = name1;
        this.name2 = name2;
        this.adjective1 = adjective1;
        this.adjective2 = adjective2;
        this.adjective3 = adjective3;
        this.verb1 = verb1;
        this.noun1 = noun1;
        this.noun2 = noun2;
        this.noun3 = noun3;
        this.noun4 = noun4;
        this.noun5 = noun5;
        this.noun6 = noun6;
        this.number = number;
        this.place1 = place1;
    }
1 Like

See its solved. How ever from what I interpret , the problem or issue was variables were attempted to be utilized prior to being declared. Hence why it worked after declaring and initializing variables that were part of String declaration after .

Hey man. I ve this 1 error. I dont understand what i have missed.
Could you please help?

MadLibs.java:1: error: ‘{’ expected
public class MadLibs.java{
^
1 error

Hello @yashodhanbarapatre83, welcome to the forums! When you create a class, the syntax is as follows:

public class someClass{}

Directly after (excluding spaces) the name of the class, Java expects an opening brace {. So the above is fine, and so is this:

public class someClass {} 

But this is not:

public class someClass;{}

That will throw a similar error to the one you’re experiencing (error: '{' expected).

Thanks @codeneutrino. It worked

1 Like

I’m stuck on this project if someone could help me, I tried the solutions posted here and had no solution. This is my code:

public class MadLibs { /* This program generates a mad-libbed story. Author: A Date 04/07/2023 */ public static void main(String[] args){ String name1 = " "; String adjective1 = " "; String adjective2 = " "; String adjective3 = " "; String verb1 = " "; String noun1 = " "; String noun2 = " "; String noun3 = " "; String noun4 = " "; String noun5 = " "; String noun6 = " "; String name2 = " "; int number = 2; String place1 = " "; } //The template for the story String story = "This morning "+name1+" woke up feeling "+adjective1+". 'It is going to be a "+adjective2+" day!' Outside, a bunch of "+noun1+"s were protesting to keep "+noun2+" in stores. They began to "+verb1+" to the rhythm of the "+noun3+", which made all the "+noun4+"s very "+adjective3+". Concerned, "+name1+" texted "+name2+", who flew "+name1+" to "+place1+" and dropped "+name1+" in a puddle of frozen "+noun5+". "+name1+" woke up in the year "+number+", in a world where "+noun6+"s ruled the world."; System.out.println(story); }

And these are the errors I got

MadLibs.java:5: error: class, interface, or enum expected
public static void main(String args){
^
MadLibs.java:9: error: class, interface, or enum expected
String adjective1 = " ";
^
MadLibs.java:11: error: class, interface, or enum expected
String adjective2 = " ";
^
MadLibs.java:13: error: class, interface, or enum expected
String adjective3 = " ";
^
MadLibs.java:15: error: class, interface, or enum expected
String verb1 = " ";
^
MadLibs.java:17: error: class, interface, or enum expected
String noun1 = " ";
^
MadLibs.java:19: error: class, interface, or enum expected
String noun2 = " ";
^
MadLibs.java:21: error: class, interface, or enum expected
String noun3 = " ";
^
MadLibs.java:23: error: class, interface, or enum expected
String noun4 = " ";
^
MadLibs.java:25: error: class, interface, or enum expected
String noun5 = " ";
^
MadLibs.java:27: error: class, interface, or enum expected
String noun6 = " ";
^
MadLibs.java:29: error: class, interface, or enum expected
String name2 = " ";
^
MadLibs.java:31: error: class, interface, or enum expected
int number = 2;
^
MadLibs.java:33: error: class, interface, or enum expected
String place1 = " ";
^
MadLibs.java:34: error: class, interface, or enum expected
}
^
MadLibs.java:38: error: class, interface, or enum expected
System.out.println(story);
^
MadLibs.java:39: error: class, interface, or enum expected
}
^
17 errors
Thanks in advanced

Kindly define the values of variable. Please dont leave it blank

For example if you type
String name1 = " "; <---- This shall give you error
String name1 = “Adam”; <----- This shall run the code successfully as the variable “String name1” has a defined value

Hope this will solve your problem!