Search code examples
javaeclipsejcreator

Unknown error on if, else if, else statement


I keep getting a syntax error on my last else statement in a string of if, else if , else , and I can't figure out why nor can I find anywhere that tells me what's really confusing is I have done this type of if else statement setup before and never had this problem. I have tried using 2 coding programs (JCreator and Eclipse) but they both give me an error Eclipse gives me a syntax error on the word else and JCreator on the entire statement saying it has no if statement to pair with it, but I have done one like this and didn't need an if statement as is was the final one.

    import java.util.Scanner;

    public class Hotel 
    {
        public static void main(String[] args)
        {
            String answer = "";
            Scanner keyboard = new Scanner(System.in);
            GuestInterface go = new GuestInterface();
            PassswordField hotel = new PassswordField();

            System.out.println("do you wish do acces the guest interface or the hotel staff interface? ");
            System.out.println("Hint: hotel staff -or- guest");
            answer = keyboard.nextLine();
            if(answer.equalsIgnoreCase("hotel Staff"))
            {
                System.out.println("Enter username");
                answer = keyboard.nextLine();
                hotel.readPassword("Enter Passsword \n ");
                System.out.print("\n" + "incorect Password");
                System.out.println("System will now shut down");
                System.exit(0);
            }
            else
            if(answer.equalsIgnoreCase("Guest"))
            {
                go.run();
            }
            else //error throws on this statment
            {
                System.out.println("uncompatible responce");
                System.out.println("System ato shut down activated");
                System.exit(0);
            }
        }
    }

Solution

  • You wrote a ; at the end of this line:

    if(answer.equalsIgnoreCase("Guest"));
    

    So that is a seperate if-statement. Right after that, you start with this:

            {
                go.run();
            }
            else //error throws on this statment
            {
                // (...)
            }
    

    Which makes no sense to the compiler, because it didn't start with an if-statement.
    Remove the ; to solve the error.