0

I'm getting the errors: illegal start of expression and 'else' without 'if' but can't find any syntax errors here.

    public int faultyMethod2(int a, int b) {
    int result;

    if((a == 0) || b > 0)) {
        result = (b / a);
    }
    else if (a < 0) {
        result = (a + b);
    }

    return result;
}
1
  • unbalanced parenthesis in the first if. delete one of the ) Commented Nov 4, 2015 at 2:57

1 Answer 1

1

You just have a typo at if((a == 0) || b > 0)) { that should be if((a == 0) || (b > 0)) {. You missed a bracket.

And later on you need to have the default value of result. Either you can give the in declaration part or you need to provide an else part and give the default value there.

 public int faultyMethod2(int a, int b) {
    int result = 0;

    if((a == 0) || b > 0)) {
        result = (b / a);
    }
    else if (a < 0) {
        result = (a + b);
    }

return result;

}

or

 public int faultyMethod2(int a, int b) {
    int result;

    if((a == 0) || b > 0)) {
        result = (b / a);
    }
    else if (a < 0) {
        result = (a + b);
    }else {
       result = 0;
    }

    return result;
}
Sign up to request clarification or add additional context in comments.

3 Comments

That will be the OP's next problem. The current problem is the extra ).
@PaulBoddington He missed a bracket.
Missed a start paren, or an end paren too many, either way they are unbalanced. Next issue: if (a == 0) { result = b / a } causes division by zero. But then again, OP did name it faultyMethod2. ;-)

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.