Reputation: 35
My code is probably amateur to a lot of you, so if any of my logic in it is messed up, that's okay. I'll fix that later. Just wondering if anyone could let me know why my else statement is coming up with: "Exception in thread "main" java.lang.Error: Unresolved compilation problem: Syntax error on token "else", delete this token"
I read some of the other questions on here and usually the problem is people are checking conditions (else (blah < bleh) {) with else, but I didn't do that.
import java.util.Scanner;
public class minOfThree {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner kb = new Scanner (System.in);
int num1, num2, num3, num4, min = 0;
System.out.println ("Please enter three numbers.");
System.out.print ("First value: ");
num1=kb.nextInt();
System.out.print ("Second value: ");
num2=kb.nextInt();
System.out.print ("Third value: ");
num3=kb.nextInt();
System.out.print ("Fourth value: ");
num4=kb.nextInt();
if (num1 < num2)
if (num1 < num3)
min=num1;
else
min=num3;
else if (num2 < num3)
min=num2;
else
min = num3;
***else*** {
min = num4;
}
System.out.println ("Minimum value is: " + min);
}
}
Upvotes: 1
Views: 1325
Reputation: 28198
Get into the habit of always writing if-else statements within brackets, then you will never have to wonder what statement goes with which test. I believe the following is what you want your code to look like:
if (num1 < num2) {
if (num1 < num3) {
min = num1;
} else {
min = num3;
}
} else {
if (num2 < num3) {
min = num2;
} else {
min = num3;
}
} else {
min = num4;
}
Although that does not make full sense because here you get two else
s as well.
Upvotes: 3
Reputation: 338
Look at the first if statement. You didn't put the braces and other mistakes you made. like putting two else statements to one if statement.
import java.util.Scanner;
public class StackOver {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner kb = new Scanner (System.in);
int num1, num2, num3, num4, min = 0;
System.out.println ("Please enter three numbers.");
System.out.print ("First value: ");
num1=kb.nextInt();
System.out.print ("Second value: ");
num2=kb.nextInt();
System.out.print ("Third value: ");
num3=kb.nextInt();
System.out.print ("Fourth value: ");
num4=kb.nextInt();
if (num1 < num2){
if (num1 < num3)
min=num1;
else
min=num3;
}else if (num2 < num3)
min=num2;
else if(num4 < num3)
min = num4;
else
min = num3;
System.out.println ("Minimum value is: " + min);
}
}
Upvotes: 1
Reputation: 8945
Your logic is amateur but it's ok? You are using two Else statements. That makes no sense. And you don't have brackets encapsulating the routines which is really confusing. What are you even trying to do? Are you trying to find the smallest number? Try this?
public static void main(String[] args){
int num1 = 4;
int num2 = 2;
int num3 = 3;
int num4 = 10;
int array[] = {num1, num2, num3, num4};
for (int i = 0; i < array.length; i++)
{
for (int j = 0; j < array.length-1; j++)
{
if (array[j] > array [j+1])
{
int x = array[j];
array[j] = array [j+1];
array [j+1] = x;
}
}
}
for (int i = 0; i < array.length; i++)
{
System.out.println(array[i]);
}
}
Upvotes: 2
Reputation: 6525
As per my knowledge if you are not writing multiple lines inside a scope (i.e. if-else statment), then there is not required of {}
.
if (num1 < num2) //1st If Start
if (num1 < num3) //2nd If Start
min=num1;
else
min=num3; //2nd If Ends
else if (num2 < num3) //3rd If starts in else part of 1st If
min=num2;
else
min = num3; //3rd If Ends
***else*** { //This else part is not followed by any If Statement (Causes Error )
min = num4;
}
In this case your approach is correct but, you missed the start point of this last else.
Hope it will help you.
Upvotes: 2
Reputation: 11132
This could have been avoided if you used {
and }
properly:
if(num1 < num2) //"if" for if-else #1
{
if(num1 < num3) //"if" for if-else #2
{
min=num1;
}
else //"else" for if-else #2
{
min=num3;
} //complete end of if-else #2
}
else if(num2 < num3) //"else if" for if-else #1
{
min=num2;
}
else //"else" for if-else #1
{
min = num3;
} //complete end of if-else #1
else //"else" for nonexistent if-else == error
{
min = num4;
}
Here you can see that you have two else
s on one if-else statement.
To fix it, use something like this:
min = Integer.MAX_VALUE; //minimum = maximum possible
if(num1 < min) //if num1 is less than current minimum...
{
min = num1; //current minimum = num1
}
if(num2 < min) //if num2 is less than current minimum...
{
min = num2; //current minimum = num2
}
if(num3 < min) //if num3 is less than current minimum...
{
min = num3; //current minimum = num3
}
if(num4 < min) //if num4 is less than current minimum...
{
min = num4; //current minimum = num4
}
Upvotes: 8
Reputation: 97
If it were me, I would just use braces and move on ;-)
if () {
// hack
} else if () {
// hack hack
} else {
// hack hack
}
However, the logic you use is wrong, too, you could replace your last else by
if (min > num4) {
min = num4;
}
Upvotes: 3
Reputation: 438
It happens because the else if
doesn't create a new if/else branch. This is the correct indentation of how compiler interprets it.
if (num1 < num2)
if (num1 < num3)
min=num1;
else
min=num3;
else if (num2 < num3)
min=num2;
else
min = num3;
else
min = num4;
As you can see, it is not what you intended. So, as only one else
statement is allowed, it doesn't compile. As already suggested, you should use braces.
Upvotes: 3
Reputation: 6570
In this case yes because there's no sense of a second else clause. It would be unreachable code
Upvotes: 1
Reputation: 5854
There can only be one else for each if/else if. The else if (num2 < num3)
part already has an else
statement two lines below it. Maybe you are missing an if-statement somewhere?
Upvotes: 7