Java Variables MadLibs won't print out String story

I am unable to get System.out.println to work to print out the story. I have this as the story:

public class MadLibs {
This program will generate a mad libbed story.
Author: Fico
Date: 1/21/2021
public static void main(String args){

  String name1 = "Katnip";
  String adjective1 = "great";
  String adjective2 = "wonderful";
  String adjective3 = "pissed";

  String verb1 = "be triggered";

  String noun1 = "alien";
  String noun2 = "Karens";
  String noun3 = "CMFTs";
  String noun4 = "Pope";
  String noun5 = "Swedish Christmas soda";
  String noun6 = "Weeabo";

  String name2 = "Mama Nip";

  int number = 2077;

  String place1 = "Tampa";


  //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.";


This is the error I am getting: error: cannot find symbol
symbol: variable story
location: class MadLibs
1 error

I declared all the variables and they all cleared but when I try to get String story to print out, it doesn’t work.

In Java, you need to define variables before you use them. Since you declare story after you print it, it is considered undefined at the System.out.println() statement.

So “story” hasn’t been defined yet? I figured it was as “String story equals the long statement” was already a defined variable. I didn’t see in the steps that String story needed more defining either so I was a little lost. It even crossed my mind but I wasn’t sure. I will give it a try later.

story is defined, just no when you want to print it. See, in the code, you have

Here, since the code runs from top to bottom, it encounters the print before it encounters the variable you’re trying to print.


Imagine taking a test on a subject you’d never heard about, then learning about it after the test. That’s what the code is like; trying to print something before it knows about the thing it is trying to print.

That makes a lot of sense! And it worked! Thank you very much. Makes sense if you see it from the run in order sort of deal. You can declare it but the commands have to come afterwards for print outs. Thank you.

1 Like