Click here to Skip to main content
15,039,244 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
import java.util.Scanner;
public class seatwork4
{
	public static void main (String [] args)
	{
		System.out.println ("Give at least 3 numbers: ");
		Scanner xd = new Scanner (System.in);

		double num1 = xd.nextDouble();
		double num2 = xd.nextDouble();
		double num3 = xd.nextDouble();
		double largest,smallest,middle;

		System.out.println ("1st number: " + num1);
		System.out.println ("2nd number: " + num2);
		System.out.println ("3rd number: " + num3);

		if (num1 > num2){
		if (num1 > num2)
		{
			largest = num1;
				if (num2 > num3)
			{ 
				middle = num2;
				smallest = num3;
			}
			else 
			{
				middle = num3;
				smallest = num2;
			}
			else
			{
				largest = num3;
				middle = num1;
				smallest = num2;
			}
		}
			
				

		}	
		else {
			if (num2 > num1)
			{
				if (num2 > num3)
				{
				 	largest = num2;
				}
				if (num1 > num3);
				{
					middle = num1;
					smallest = num3;
				}
				else
				{
					middle = num3;
					smallest = num1;
				}						
				else 
				{
					largest = num3;
					middle= num2;
					smallest= num1;
				}
			}
		}	
	
	}		
	
}


What I have tried:

I've tried re-coding but it still has the same error.
The three integers that the user will input should be placed in ascending and descending order.
Posted
Updated 17-Feb-21 6:56am
v2

Fix your code indentation, and then examine your if..else blocks:
C++
if (num1 > num2)
{
    if (num1 > num2)
    {
        ...
        if (num2 > num3)
        { 
            ...
        }
        else 
        {
            ...
        }
        else // <-- You've already provided an "else" block for this "if" statement.
        {
            ...
        }
    }
}   
else 
{
    if (num2 > num1)
    {
        if (num2 > num3)
        {
            ...
        }
        if (num1 > num3);
        {
            ...
        }
        else
        {
            ...
        }                       
        else // <-- You've already provided an "else" block for this "if" statement.
        {
            ...
        }
    }
}   
You have two if blocks with multiple else branches, which is not allowed.
   
Comments
Nelek 17-Feb-21 7:14am
   
that's why I like to put brackets below the key word and indent properly. The layout can be really helpful to spot some problems.
Member 15074855 17-Feb-21 7:24am
   
Hey, I tried fixing the code and it run now. However the output didn't list the three integers into ascending and descending numbers. Can I get an advice?
Member 15074855 17-Feb-21 7:25am
   
import java.util.Scanner;
public class seatwork4
{
public static void main (String [] args)
{
System.out.println ("Give at least 3 numbers: ");
Scanner xd = new Scanner (System.in);

double num1 = xd.nextDouble();
double num2 = xd.nextDouble();
double num3 = xd.nextDouble();
double largest,smallest,middle;

System.out.println ("1st number: " + num1);
System.out.println ("2nd number: " + num2);
System.out.println ("3rd number: " + num3);

if (num1 > num2)
{
if (num1 > num2)
{
largest = num1;
if (num2 > num3)
{
middle = num2;
smallest = num3;
}
else
{
middle = num3;
smallest = num2;
}

{
largest = num3;
middle = num1;
smallest = num2;
}
}
}
else
{
if (num2 > num1)
{
if (num2 > num3)
{
largest = num2;
}
if (num1 > num3);
{
middle = num1;
smallest = num3;
}

{
middle = num3;
smallest = num1;
}

{
largest = num3;
middle = num2;
smallest = num1;
}
}
}

}

}

here's what I did
Richard Deeming 17-Feb-21 7:41am
   
You've just removed the second else keyword from each block. The code within the braces will always execute, overwriting the variables you've just set in your if..else block.

Stop making random changes to your code. Get a piece of paper and a pen, and write down exactly what you want the code to do, step by step. Then write the code to do that.
You cannot have more than one else clause for an if statement. If you need to test for multiple conditions then you need to use else if or a switch block.

And in your code you have:
Java
if (num1 > num2){
if (num1 > num2) // why are you repeating this if statement?
{
    largest = num1;
    if (num2 > num3)
    {
        middle = num2;
        smallest = num3;
    }
    else
    {
        middle = num3;
        smallest = num2;
    }
    else // you cannot have a second else clause
    {
        largest = num3;
        middle = num1;
        smallest = num2;
    }
}
   
You are chaining two elses together:
if(condition)
{
   do A
}
else
{
   do B
}
else  // How can the code expect to get here? It makes no sense!
{
   do C
}

I haven't studied your code beyond that, to see what you're trying to do, but it could be that you need to add an if after the first else. The following makes sense:
if(condition)
{
   do A
}
else if(condition)
{
   do B
}
else
{
   do C
}
   
v2
You need to start sorting these syntax errors for yourself - you really didn't spend any significant time trying to fix this, given the time different between this and your previous syntax error ridden question / answers.

Syntax errors are part of life: they mean you typed it wrong and are simple to fix.
Just look at the error message and it will show you the file name, the line and column it found an error on, and give you a message describing the error.
So look at that line and consider what the message is trying to tell you: "'else' without 'if'"
That means it finds an else clause that can't be "tied up" with an if clause - and the syntax of an if statement in Java is simple:
Java
if (condition1) {
  // block of code to be executed if condition1 is true
} else if (condition2) {
  // block of code to be executed if the condition1 is false and condition2 is true
} else {
  // block of code to be executed if the condition1 is false and condition2 is false
}

So all you have to do is look at the code above the line it found the problem on and see where the if that you think it matches is, and why it isn't "lining up" correctly.

And there are two reasons why in that code: a spurious semicolon you haven't noticed yet, and an else that is all alone.

Fix that one error, and compile again - the chances are many errors will go away at the same time!


Give it a try: you are going to be fixing syntax errors for a long time, and it's much, much quicker to be able to solve them yourself that it is to post for help on trivial matters like this and have to wait for a reply!
   
Not the 'else' error, but:
Java
if (num2 > num1)
{
    if (num2 > num3)
    {
        largest = num2;
    }
    if (num1 > num3); // the semicolon here is an error
    {
        middle = num1;
        smallest = num3;
    }
    else
    {
        middle = num3;
        smallest = num1;
    }
    else
    {
        largest = num3;
        middle= num2;
        smallest= num1;
    }
}

-----
Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
Java
import java.util.Scanner;
public class seatwork4
{
	public static void main (String [] args)
	{
		System.out.println ("Give at least 3 numbers: ");
		Scanner xd = new Scanner (System.in);

		double num1 = xd.nextDouble();
		double num2 = xd.nextDouble();
		double num3 = xd.nextDouble();
		double largest,smallest,middle;

		System.out.println ("1st number: " + num1);
		System.out.println ("2nd number: " + num2);
		System.out.println ("3rd number: " + num3);

		if (num1 > num2){
			if (num1 > num2)
			{
				largest = num1;
				if (num2 > num3)
				{
					middle = num2;
					smallest = num3;
				}
				else
				{
					middle = num3;
					smallest = num2;
				}
				else
				{
					largest = num3;
					middle = num1;
					smallest = num2;
				}
			}



		}
		else {
			if (num2 > num1)
			{
				if (num2 > num3)
				{
					largest = num2;
				}
				if (num1 > num3);
				{
					middle = num1;
					smallest = num3;
				}
				else
				{
					middle = num3;
					smallest = num1;
				}
				else
				{
					largest = num3;
					middle= num2;
					smallest= num1;
				}
			}
		}

	}

}

Indentation style - Wikipedia[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
Enabling Open Innovation & Collaboration | The Eclipse Foundation[^]
-----
Your code is over complicated for what you want to do, try this:
Java
if (num1 > num2){
    largest = num1;
    middle = num2;
}
else {
    largest = num2;
    middle = num1;
}
if (middle > num3){
    smallest = num3;
}
else {
    smallest = middle;
    if (largest > num3){
        middle = largest;
        largest = num3;
    }
    else {
        middle = num3;
    }
}
   
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900