1

I am creating a text-based game and, so far, I have gotten the if and the else statements to work. However, the else if does not work. I've gone through several sites and several pages and even talked with a fellow coder. I've done what I can, such as replacing == with "?".equals(Call#)) then changing to Call#.equals("?"). I had the else if as a nested if but that also failed.

public static void FirstRoom(){
    System.out.println("You are in a room filled with\ndarkness and no memory of your\npast. There are several objects\nscattered on the ground and a\nlantern in your hand. You find\na lighter in your pocket.");
    System.out.println("\n ----- \n");
    Scanner console = new Scanner(System.in);
    String Call = console.next();
    if (Call.equals("Inspect")) {
        System.out.println("\nYou can only see darkness");
        System.out.println("\n ----- \n");
    } else if(Call.equals("Light Lantern")){
        System.out.println("\nYou pull the lighter from your pocket and,\nturning the knob, raise the\nlantern's wick and light it.");
    System.out.println("\n ----- \n");
    }else{
        System.out.println("\nWhat do you mean?");
        System.out.println("\n ----- \n");
    FirstRoom();
   }
   String Call2 = console.next();
   if(Call.equals("Inspect")) {
       System.out.println("You see a large sword to your\nright and a bag to the left.\nThere is a door in front\n of you and a door behind.");
       System.out.println("\n ----- \n");
   }
}

When I type in Light Lantern, I expect it to say You pull the lighter from your pocket and, turning the knob, raise the lantern's wick and light it. Instead, it skips to the second statement (supposed to also wait for you to input for the second) and says You see a large sword to your right and a bag to your left. There is a door in front of you and a door behind.

Inspect

You can only see darkness


Light Lantern You see a large sword to your right and a bag to your left. There is a door in front of you and a door behind.


j08691
  • 190,436
  • 28
  • 232
  • 252
JGTT
  • 9
  • 6
  • 2
    issue 1: that's not javascript – Jaromanda X Dec 06 '17 at 00:04
  • Also, if you choose not to follow Java variable [naming conventions](http://www.oracle.com/technetwork/java/codeconventions-135099.html) (i.e. all variables start with lower case letter) you might end up calling a static function of some external class with some auto-import IDEs. – urgentx Dec 06 '17 at 00:20

2 Answers2

4

You need to use console.nextLine() not console.next()

String Call /*bad name*/ = console.next();

Will only take the first word "Light"

String call = console.nextLine(); //using normal JAVA naming conventions

Will take everything up to the the end of line character which means you will be able to input multiple words like "Light Lantern"

For more information on the difference between next() and nextLine() you can check out this post

RAZ_Muh_Taz
  • 3,964
  • 1
  • 10
  • 22
0

You are stopping to take input at String Call2 = console.next(); before checking if Call is equal to "Inspect". You should be checking Call2.

This would be my suggestion to reworking that method:

public static void FirstRoom() {
    System.out.println("You are in a room filled with\ndarkness and no memory of your\npast. There are several objects\nscattered on the ground and a\nlantern in your hand. You find\na lighter in your pocket.");
    System.out.println("\n ----- \n");
    Scanner console = new Scanner(System.in);
    String Call;
    boolean exitRoom = false;
    boolean lantern = false;
    while(!exitRoom) {
        Call = console.nextLine();
        if(Call.equals("Inspect") && !lantern) {
            System.out.println("\nYou can only see darkness");
            System.out.println("\n ----- \n");
        } else if(Call.equals("Light Lantern")) {
            System.out.println("\nYou pull the lighter from your pocket and,\nturning the knob, raise the\nlantern's wick and light it.");
            System.out.println("\n ----- \n");
            lantern = true;
        } else if(Call.equals("Inspect") && lantern) {
            System.out.println("You see a large sword to your\nright and a bag to the left.\nThere is a door in front\n of you and a door behind.");
            System.out.println("\n ----- \n");
        } else if(Call.equals("Leave")) {
            System.out.println("You leave the room");
            System.out.println("\n ----- \n");
            exitRoom = true;
        } else {
            System.out.println("\nWhat do you mean?");
            System.out.println("\n ----- \n");
            FirstRoom();
        }
    }
}

When a command to leave the room is requested, the while loop ends.

Alea Kootz
  • 895
  • 4
  • 11
  • Thank you for your answer. However, this is a project I'm using to learn the if-else if-else loops. As of the moment, I have yet to use a while loop. However, I will look into them. Once more, thank you. – JGTT Dec 06 '17 at 00:59
  • This while loop simply makes it so that you can put in place all of the various call-response behavior without needing to duplicate any of the code. – Alea Kootz Dec 06 '17 at 01:03
  • Try using it, I think you'll find the behavior to be what you expect. – Alea Kootz Dec 06 '17 at 01:04