-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
    `if (negative && (a < 0 && b < 0));` <-- Get rid of the trailing `;` from both the `if` and `else if` statements – MadProgrammer Jan 13 '19 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}`. – Pshemo Jan 13 '19 at 21:40

1 Answers1

-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
        .....
}
rei
  • 37
  • 1
  • 6