0

For my class I have to create a interactive story/game. I a having some trouble with the variables. This is my first coding class I have ever taken so I am sorry if it looks messy. I put alot of it into ifs and else ifs because that is all we have learned so far. My error is mostly coming from else if (Go.equalsIgnoreCase("Speak")) { I am getting the error "This variable my have not been initialized.
Please dumb your responses down if possible because I am still trying to learn it all. Thank you.

import java.util.Scanner;

public class Homework02 {

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);

    String Go, Listen, Speak, Fight, A, B, C, D;

    System.out.println(" WELCOME TO THE DUNGEON!");
    System.out.println("  ");
    System.out.println(" You are woken up by a large man kicking you in the stomach.\n"
            + "You let a large gasp of air out before proptly getting up in a blurry state.\n"
            + "What will you do?\n"
            + "(------------------------------------------)\n"
            + "1)Listen to the man (Listen)\n"
            + "2)Speak to the man (Speak)\n"
            + "3)Throw a punch at the man(Attack) ");
    System.out.print("> ");
    Go = keyboard.next();

    if (Go.equalsIgnoreCase("Listen")) {
        System.out.println("The large man tells you to get ready for your hearing in 2 minutes.\n"
                    + "What will you do?\n"
                    + "(---------------------------------------------------------------------)\n"
                    + "1)Attack(1)\n"
                    + "2)Sit Down(2) ");}
        
        
        else if (Go.equalsIgnoreCase("Speak")) {
            System.out.println("You open your mouth to start speaking to the large man.\n"
                    + "As you make a sound he balls his fist up and sucker punches you in the stomach dropping you to the floor.\n"
                    + "He states 'You speak when spoken to you filthy mutt.' Then tells you to get ready for your hearing\n"
                    + "You spit out blood onto the floor and get on all fours. The large man kicks your head into the toilet and your vision gets fuzzy\n"
                    + "You slowly start to fade into an unconcious state. As you start to see black the man says 'That's one less piece of trash we have to feed now.'");
            System.out.println("////YOU HAVE DIED. RESTART THE GAME.////");
        
        
      A = keyboard.next();
       
      if (A.equalsIgnoreCase("1"));
    System.out.println("The man turns his back to walk out of your cell.\n"
            + "As the man begeins to walk out you grab your blanket and rush him.\n"
            + "You wrap the blanket around his neck and start to strangle him.\n"
            + "The large man begins to turn purple and blue and you keep pulling tighter and tighter.\n"
            + "The man finally falls to his knees then onto his belly.\n"
            + "As the large man lies lifeless on the floor you grab his baton and keys and run out of the cell.\n"
            + "Do you run to the Left or to the Right?\n"
            + "(--------------------------------------------------------------------------)\n"
            + "1)Left\n"
            + "2)Right");}
        else if (A.equalsIgnoreCase("2"));
    System.out.println("You sit down to think about how you ended up there but you are drawing blanks.\n"
            + "You don't even know your name.\n"
            + "You sit on the floor of the cell waiting for the Large man to come back and retreive you.\n"
            + "*2 minutes pass by*\n"
            + "The large man appears again and tells you to follow him.\n"
            + "As you follow the large man down the hallway of the dungeon you see all the other starving prisoners rotting away in their cells.\n"
            + "You finally arrive to a large room filled with torches and skulls with a man sitting on a throne made of bones\n"
            + "'AHHHHH! My newest warrior!' The man exclaims! 'I cannot wait to see what you can do in the arena!'\n"
            + "'We have your first round tomorrow! Please eat and rest up for your fight! I cant wait to see you in action!'"
            + "What do you do?\n"
            + "(---------------------------------------------------------------------------------------------------------------------)"
            + "1)Go back to your cell to eat and rest(1)\n"
            + "2)Speak\n");
            
    
    
    
    
    }
{
    
   
    

    }

}

Josi Whitlock
  • 185
  • 4
  • 15
OdinsGrim
  • 3
  • 1
  • Try initializing your `Go` variable. This means setting it to an initial value after declaring it. Your error is because you're comparing the `Go` string which may not have a value. Try using `String Go = "";` in your `Go` declaration and see if that works. – Josi Whitlock Feb 26 '21 at 01:37
  • This might be helpful for your question. [Variable might not have been initialized error](https://stackoverflow.com/questions/2448843/variable-might-not-have-been-initialized-error) – BurdenKing Feb 26 '21 at 01:39
  • @OboeWanKenobi When I use that it gives me the error "A Cannot be Resolved" – OdinsGrim Feb 26 '21 at 01:51
  • @OdinsGrim I'll pop it into an IDE and take a look. – Josi Whitlock Feb 26 '21 at 01:56
  • Also, for future reference, at the end of your program, you'll want to use `keyboard.close();` to close your scanner to avoid a resource leak. – Josi Whitlock Feb 26 '21 at 02:00
  • @OdinsGrim I put it in VSCode and set both `A` and `Go` to empty string literals (`""`). It seems to run just fine: https://pastebin.pl/view/c16487d4 – Josi Whitlock Feb 26 '21 at 02:07

2 Answers2

1

Its a good practice to check if a variable has been defined before doing if/else statements that involve that variable. This would involve encasing your current if/else statements in one if statement, which does this check:

Go = keyboard.next();

if (Go != null) { 
//insert your code here
}

if you also need to check for empty strings, check out this page see: https://www.programiz.com/java-programming/examples/string-empty-null

Mihailo
  • 26
  • 3
0

Because the next() method returns a String object which could be null if no token is found. See https://www.tutorialspoint.com/java/util/scanner_next.htm

So you have to check if Go is not null before continuing to work on it.

    if (Go != null) {  // this check is important
        if (Go.equalsIgnoreCase("Listen")) {
        System.out.println("The large man tells you to get ready for your hearing in 2 minutes.\n"
                    + "What will you do?\n"
                    + "(---------------------------------------------------------------------)\n"
                    + "1)Attack(1)\n"
                    + "2)Sit Down(2) ");}
        
        
        else if (Go.equalsIgnoreCase("Speak")) {
            System.out.println("You open your mouth to start speaking to the large man.\n"
                    + "As you make a sound he balls his fist up and sucker punches you in the stomach dropping you to the floor.\n"
                    + "He states 'You speak when spoken to you filthy mutt.' Then tells you to get ready for your hearing\n"
                    + "You spit out blood onto the floor and get on all fours. The large man kicks your head into the toilet and your vision gets fuzzy\n"
                    + "You slowly start to fade into an unconcious state. As you start to see black the man says 'That's one less piece of trash we have to feed now.'");
            System.out.println("////YOU HAVE DIED. RESTART THE GAME.////");
        
        
      A = keyboard.next();
       
      if (A.equalsIgnoreCase("1"));
    System.out.println("The man turns his back to walk out of your cell.\n"
            + "As the man begeins to walk out you grab your blanket and rush him.\n"
            + "You wrap the blanket around his neck and start to strangle him.\n"
            + "The large man begins to turn purple and blue and you keep pulling tighter and tighter.\n"
            + "The man finally falls to his knees then onto his belly.\n"
            + "As the large man lies lifeless on the floor you grab his baton and keys and run out of the cell.\n"
            + "Do you run to the Left or to the Right?\n"
            + "(--------------------------------------------------------------------------)\n"
            + "1)Left\n"
            + "2)Right");}
        else if (A.equalsIgnoreCase("2"));
    System.out.println("You sit down to think about how you ended up there but you are drawing blanks.\n"
            + "You don't even know your name.\n"
            + "You sit on the floor of the cell waiting for the Large man to come back and retreive you.\n"
            + "*2 minutes pass by*\n"
            + "The large man appears again and tells you to follow him.\n"
            + "As you follow the large man down the hallway of the dungeon you see all the other starving prisoners rotting away in their cells.\n"
            + "You finally arrive to a large room filled with torches and skulls with a man sitting on a throne made of bones\n"
            + "'AHHHHH! My newest warrior!' The man exclaims! 'I cannot wait to see what you can do in the arena!'\n"
            + "'We have your first round tomorrow! Please eat and rest up for your fight! I cant wait to see you in action!'"
            + "What do you do?\n"
            + "(---------------------------------------------------------------------------------------------------------------------)"
            + "1)Go back to your cell to eat and rest(1)\n"
            + "2)Speak\n");        
        }
    }