-8

so here is my code.

public static void play(Player player) {
    Scanner localScanner = new Scanner(System.in);
    while(localScanner.hasNextLine()){
      String input = localScanner.nextLine();
      if(input.equals("quit")) {
        System.out.println("Game over, Good bye.");
        return; 
      }
      if(input.contains("north")){
        buildWorld(player, player.moveTo().connectNorth());
      } else if(input.contains("south")){
        buildWorld(player, player.moveTo().connectSouth());
      } else if(input.contains("east")){
        buildWorld(player, player.moveTo().connectEast());
      } else if(input.contains("west")){
        buildWorld(player, player.moveTo().connectWest());
      } else {
        String contents;
        if (!contents.equals(""))
          System.out.println("There is:\n" + contents);
        else{
          System.out.println("This room is empty.");
       } else if(input.startsWith("pickup")){  // <-- This is the 'orphan' else
        contents = input.substring(8);
        if(player.moveTo().moveTo(contents)){
          Object localObject = player.connectWest(contents);
          if(localObject !=null)
            System.out.println("You have picked up " +localObject);
          else
            System.out.println("You have too much damage, Game over.");}
        else {
          System.out.println("There is no " +contents);
      }} else if (input.startsWith("drop")){
        contents=input.substring(6);
        if(player.moveTo(contents))
          System.out.println("you dropped " +contents);
        else
          System.out.println("You don't have "+ contents); 
      } else if(input.contains("status")){
        System.out.println(player);}
      else{
        System.out.println("What?");}
    }
  }
  } 

I keep getting an else without if error and I don't know how to fix it. the bolded code is where the error occurs. I have been working on this for hours and I still can;t get it to work. please, please, please help me.

6
  • Using standard code formatting would help. Your indentation and bracket placing is so random we can't be sure without reading everything... May 10, 2013 at 17:17
  • 2
    You just can't do an else-else if
    – BackSlash
    May 10, 2013 at 17:18
  • 1
    The standard else is basically the catch-all in an if-else chain. You can't have another else-if after it.
    – Perception
    May 10, 2013 at 17:18
  • else is the ultimatum, no else then else if swap them. May 10, 2013 at 17:19
  • Most IDEs won't even let you attempt to compile code with type of syntax error... I recommend using Eclipse as an IDE
    – Tdorno
    May 10, 2013 at 17:20

2 Answers 2

5

I think the problem is that you have an else if following an else. Try moving the else to be the last of this sequence.

Also, you have two else statements following the same if:

else {
    String contents;
    ...
}
...
else{
    System.out.println("What?");}

This will cause additional problems, and it doesn't even make sense, really.

2

This part is the problem:

    if (!contents.equals(""))
      System.out.println("There is:\n" + contents);
    else{
      System.out.println("This room is empty.");}
    else if(input.startsWith("pickup")) {
    // ...
    }

You can't have else if after else.

Not the answer you're looking for? Browse other questions tagged or ask your own question.