-1

I am trying to run this simple code in Netbeans and somehow keep getting an 'else without if' error.

I ran this in Eclips as well and there I get the remark that else should be converted into a while which confuses me even more. I already did some examples like this and they worked.

I also left out the semi colon at the end of the if statement but it didn't work.

public static void main(String[] args) {

    int a = 4;
    int b = 5;
    boolean negative = false;

    if (negative && (a < 0 && b < 0)); 
    {  
    System.out.println("true");
    }
    else((a < 0 && b > 0) || (a > 0 && b < 0));
    {
    System.out.println("false");
    }
2
  • 2
    if (negative && (a < 0 && b < 0)); <-- Get rid of the trailing ; from both the if and else if statements Commented Jan 13, 2019 at 11:15
  • Second problem is that else doesn't take any condition. If you want to add one you need another if(condition) like if (isBlueTest) {handle blue} else if(isRedTest) {handle red} else {handle any other cases}. Commented Jan 13, 2019 at 21:40

1 Answer 1

-1

The problem is you put a condition to an else statement. You can only put a condition on else if statements

public static void main(String[] args) {

    int a = 4;
    int b = 5;
    boolean negative = false;

    if (negative && (a < 0 && b < 0)) {  
        System.out.println("true");
    } else if((a < 0 && b > 0) || (a > 0 && b < 0)){
        System.out.println("false");
    } else
        .....
}
Sign up to request clarification or add additional context in comments.

2 Comments

Don"t say try, but explain the problem and the given solution
It still doesn't work, the error next to the 'else' says: not a statement.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.