Reputation: 11
This code sorts three numbers from smallest to greatest. However, my else statements gave me 4 errors, on lines 15, 18, 23, 26, and 31. Does anyone know why this is the case?
import java.util.*;
public class sortnumber {
public static void main (String[] args) {
Scanner scan = new Scanner (System.in);
int num1, num2, num3, random;
num1 = scan.nextInt();
num2 = scan.nextInt();
num3 = scan.nextInt();
random = 0;
if (num1 < num2 && num1 < num3)
System.out.print (num1);
if (num2 < num3)
System.out.print (num2);
System.out.print (num3);
else
System.out.print (num3);
System.out.print (num2);
else if (num2 < num1 && num2 < num3)
System.out.print (num2);
if (num1 < num3)
System.out.print (num1);
System.out.print (num3);
else
System.out.print (num3);
System.out.print (num1);
else if (num3 < num2 && num3 < num1)
System.out.print (num3);
if (num2 < num1)
System.out.print (num2);
System.out.print (num1);
else
System.out.print (num1);
System.out.print (num2);
}
}
Upvotes: 0
Views: 129
Reputation: 228
An if/else if/else statement requires curly braces ({, }) if the block of code under it is more than one line. For example, your first if statement is completely valid. However, errors lie within the rest of the code. I would suggest using curly braces almost always though, as it typically makes your code more readable (even if there is only one line under the if statement).
Upvotes: 1
Reputation: 13
You are missing curly braces.
Java code does not pay attention to where new lines start or how much indentation there is. Instead, end-of-line characters (semi-colons) and curly braces are used.
While braceless if/else statements are supported in Java, they only associate one statement with the if
or the else
. It is considered best practice to always include the curly braces around the code you wish to include in your conditional statement.
For example, taking your first nested if
statement:
if (num1 < num2 && num1 < num3) {
System.out.print (num1);
if (num2 < num3) {
System.out.print (num2);
System.out.print (num3);
}
else {
System.out.print (num3);
System.out.print (num2);
}
}
Take a look at this page for some old but useful pointers: http://www.fredosaurus.com/notes-java/flow/if/30if-braces.html
Upvotes: 0
Reputation: 159086
Since you don't have curly braces, your indentations are wrong. What you actually coded is this:
random = 0;
if (num1 < num2 && num1 < num3)
System.out.print (num1);
if (num2 < num3)
System.out.print (num2);
System.out.print (num3);
else // <===== ERROR
System.out.print (num3);
System.out.print (num2);
else if (num2 < num1 && num2 < num3) // <===== ERROR
System.out.print (num2);
if (num1 < num3)
System.out.print (num1);
System.out.print (num3);
else // <===== ERROR
System.out.print (num3);
System.out.print (num1);
else if (num3 < num2 && num3 < num1) // <===== ERROR
System.out.print (num3);
if (num2 < num1)
System.out.print (num2);
System.out.print (num1);
else // <===== ERROR
System.out.print (num1);
System.out.print (num2);
If you add the braces indicated by your indentations, you get this:
random = 0;
if (num1 < num2 && num1 < num3) {
System.out.print(num1);
if (num2 < num3) {
System.out.print(num2);
System.out.print(num3);
} else {
System.out.print(num3);
System.out.print(num2);
}
} else if (num2 < num1 && num2 < num3) {
System.out.print(num2);
if (num1 < num3) {
System.out.print(num1);
System.out.print(num3);
} else {
System.out.print(num3);
System.out.print(num1);
}
} else if (num3 < num2 && num3 < num1) {
System.out.print(num3);
if (num2 < num1) {
System.out.print(num2);
System.out.print(num1);
} else {
System.out.print(num1);
System.out.print(num2);
}
}
In order to prevent mistakes like that, it is commonly recommended to always use curly braces.
Upvotes: 3