Fix the Error: Else Without if in Java
-
the
error: 'else' without 'if'
in Java -
Reasons for
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:
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.
- The first reason is that we forgot to write the
if
block before theelse
block. - The closing bracket of the
if
condition is missing. - 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).
Related Article - Java Error
- Fix the Error: Failed to Create the Java Virtual Machine
- Fix the Missing Server JVM Error in Java
- Fix the 'No Java Virtual Machine Was Found' Error in Eclipse
- Fix the Error: Failed to Create the Java Virtual Machine
- Javax.Net.SSL.SSLHandShakeException: Remote Host Closed Connection During Handshake
- Java.Lang.VerifyError: Bad Type on Operand Stack