Fix the Error: Else Without if in Java

Mehvish Ashiq Jul 27, 2022
  1. the error: 'else' without 'if' in Java
  2. Reasons for error: 'else' without 'if' in Java
  3. Fix the error: 'else' without 'if' in Java
Fix the Error: Else Without if in Java

Today, we will learn about an error saying 'else' without 'if' while writing code in Java. We will also figure out the possible reasons causing this error and find its solution.

the error: 'else' without 'if' in Java

Usually, this kind of error is faced by newbies in Java programming. Before moving toward the causes and solution for this error, let’s write a program that produces this error and understand it.

So, assuming that we are Python experts and beginners in Java. So, we will write the Java program containing if-else as follows.

Example Code:

//import libraries
import java.util.Scanner;

//decide future activity based on the current temperature
public class Test{
    public static void main (String[] args){

        int temp;
        Scanner scan = new Scanner(System.in);
        System.out.println ("What's the current temperature?");
        temp = scan.nextInt();

        if (temp > 95 || temp < 20);
            System.out.println ("Visit our shops");
            else if (temp <= 95)
                if (temp >= 80)
                System.out.println ("Swimming");
                else if (temp >=60)
                    if (temp <= 80)
                    System.out.println ("Tennis");
                    else if (temp >= 40)
                        if (temp < 60)
                        System.out.println ("Golf");
                        else if (temp < 40)
                            if (temp >= 20)
                            System.out.println ("Sking");                                      }//end main()
}//end Test Class

Error:

fix else without if an error in java - error

In this program, we get the current temperature from the user and decide our future activity based on the current temperature. The image above shows that we are getting a logical error about which NetBeans IDE informs at compile time.

So, we cannot even execute the code until we resolve the error. For that, we will have to know the possible reasons below.

Reasons for error: 'else' without 'if' in Java

The error itself is explanatory, which says that a Java compiler cannot find an if statement associated with the else statement. Remember that the else statement does not execute unless they’re associated with an if statement.

So, the possible reasons are listed below.

  1. The first reason is that we forgot to write the if block before the else block.
  2. The closing bracket of the if condition is missing.
  3. We end the if statement by using a semi-colon.

How to solve this error? Let’s have a look at the following section.

Fix the error: 'else' without 'if' in Java

Example Code:

//import libraries
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {

        int temp;
        Scanner scan = new Scanner(System.in);
        System.out.println("What's the current temperature?");
        temp = scan.nextInt();

        if (temp > 95 || temp < 20) {
            System.out.println("Visit our shops");
        }//end if
        else if (temp <= 95) {
            if (temp >= 80) {
                System.out.println("Swimming");
            } //end if
            else if (temp >= 60) {
                if (temp <= 80) {
                    System.out.println("Tennis");
                }//end if
                else if (temp >= 40) {
                    if (temp < 60) {
                        System.out.println("Golf");
                    }//end if
                    else if (temp < 40) {
                        if (temp >= 20) {
                            System.out.println("Sking");
                        }//end if
                    }//end else-if
                }//end else-if
            }//end else-if
        }//end else-if
    }//end main()
}//end Test Class

Output:

What's the current temperature?
96
Visit our shops

We removed the semi-colon (;) from the end of the if statement and placed the {} for each block to fix an error saying 'else' without 'if'.

It is better to use curly brackets {} until we are expert enough and know where we can omit them (we can omit them when we have a single statement in the block).

Mehvish Ashiq avatar Mehvish Ashiq avatar

Mehvish Ashiq is a former Java Programmer and a Data Science enthusiast who leverages her expertise to help others to learn and grow by creating interesting, useful, and reader-friendly content in Computer Programming, Data Science, and Technology.

LinkedIn GitHub Facebook

Related Article - Java Error