1

I am writing a method which contains if else statements, but also a return keyword. Now, I'm writing something like this:

public boolean deleteAnimal(String name) throws Exception{
    if(name == null || name.trim().isEmpty())
        throw new Exception("The key is empty");
    else if(exists(name)){
        hTable.remove(name);
    }
    else throw new Exception("Animal doesn't exist");

    return hTable.get(name) == null;
}

I'm new in java and this is my first time trying to learn a programming language. I read that the 'else' statement excecutes always if the if condition is false.

Now, if these are false:

if(name == null || name.trim().isEmpty())
        throw new Exception("The key is empty");
    else if(exists(name)){
        hTable.remove(name);
}

Shouldn't the else part always excecute?

else throw new Exception("Animal doesn't exist");

I noticed this since this method is returning true/false, and it seems like it's ignoring the else part, even when the conditions above it are false.

8
  • Would you mind posting a Minimal, Complete, and Verifiable example? Commented Jun 15, 2016 at 15:46
  • The else will fire if all other conditionals--i.e. if, if else--are false. It does not always execute. Think about it like this: If it is raining, then I carry my umbrella; else if it is snowing, then I wear my parka; else I wear shorts and a tshirt. Commented Jun 15, 2016 at 15:47
  • These are not nested. They are sequenced. Your second else goes with your second if. If you want "Animal doesn't exist", exists(name) needs to be false, not the conditions in the first if-condition. Commented Jun 15, 2016 at 15:48
  • @KennethK. The OP asked if those (the other conditionals) were false, would else always execute and that would be yes Commented Jun 15, 2016 at 15:49
  • @AndrewL No, he wrote if the if condition was false, not all other conditionals. Commented Jun 15, 2016 at 15:50

2 Answers 2

1

Without the knowledge about the rest of the code exists(String name) and the type of hTable (Map<String,? extends Object>) I need to guess:

If exits returns true, the else if statement evaluates to true. The line hTable.remove(name) will be executed. The else-branch is not invoked, because the else if was. Now the last line will return hTable.get(name) == null;

I think it will return true, because the hTable will return null.

Sign up to request clarification or add additional context in comments.

Comments

1

I'll try to add comments to your snippet to help you understand the flow:

public boolean deleteAnimal(String name) throws Exception{
    if(name == null || name.trim().isEmpty())
        throw new Exception("The key is empty");   //Executes if 'name' is null or empty

    else if(exists(name)){
        hTable.remove(name);       // Excecutes if 'name' is not null and not empty and the exists() returns true
    }

    else 
        throw new Exception("Animal doesn't exist");  //Excecutes if 'name' is not null and not empty and the exists() returns false

    return hTable.get(name) == null;    //The only instance when this is possible is when the 'else if' part was executed
}

Hope the comments helps you understand the flow!

With this in mind, the answer to your question is 'yes'.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.