I'm writing a simple user input
package caleb.fournier;
import java.util.Scanner;
//This line above must be used if you're using user input
public class Caleb
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int age;
System.out.println("How old are you?");
age=in.nextInt();
if (age >= 21);
System.out.println("You are old enough to drink");
else
System.out.println("You are not old enough to drink");
}
}
And getting this error
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error on token "else", assert expected
then cannot be resolved to a variable
at caleb.fournier.Caleb.main(Caleb.java:15)
PLEASE HELP ME.
DO NOT DEAL WITH THIS MEMBER
You have a semi-colon here which should not be here..
g4143 there can be a semicolon. The issue is the lacking of a brace pair {} with the else code inside.
But yes, you are correct. If you wish to not use the braces, remove the ; from the if statement. This will throw a warning in some IDEs, but will still work.
~ Singularity ~
DO NOT DEAL WITH THIS MEMBER
(Jul 27, 2013 03:01 AM)vitafortis Wrote: This will throw a warning in some IDEs, but will still work.
Since we're being picky. Java compilers may generate a warning in this case not Java IDE's.
(Jul 27, 2013 03:12 AM)g4143 Wrote: Since we're being picky. Java compilers may generate a warning in this case not Java IDE's.
False. I compiled it that way, didn't warn me. The IDE warned me of bad practice ;)
~ Singularity ~
DO NOT DEAL WITH THIS MEMBER
(Jul 27, 2013 03:14 AM)vitafortis Wrote: False. I compiled it that way, didn't warn me. The IDE warned me of bad practice ;)
Odd, my compiler generated this.
Code Caleb.java:14: error: 'else' without 'if'
else
^
1 error
(Jul 27, 2013 03:26 AM)g4143 Wrote: Odd, my compiler generated this.
Code Caleb.java:14: error: 'else' without 'if'
else
^
1 error
Oh well that's the error, yes. But the warning I am speaking of is lack of braces and lack of semicolon, such as:
if(i == 5)
System.out.println("Hello");
The above compiles, but IDE's claim bad practice. The error you are referring to is obvious as the OP declared a singular if statement without any else or if-else statements. I feel we had a disconnect between what we were "nit-picking" about. I apologize for that.
~ Singularity ~
Jul 27, 2013 09:58 AM
(This post was last modified: Jul 27, 2013 09:59 AM by Kristoff.)
Don't do a semicolon after an IF.
if(boolean expression){
statement();
}else{
statement();
}
That should fix it. The semicolon essentially ends that IF statement the same way an IF without grouping symbols does upon the first expression.
(Jul 27, 2013 06:37 AM)vitafortis Wrote: Oh well that's the error, yes. But the warning I am speaking of is lack of braces and lack of semicolon, such as:
if(i == 5)
System.out.println("Hello");
The above compiles, but IDE's claim bad practice. The error you are referring to is obvious as the OP declared a singular if statement without any else or if-else statements. I feel we had a disconnect between what we were "nit-picking" about. I apologize for that.
Lul my compiler would throw me an error for that let alone a warning.
(Jul 26, 2013 11:02 PM)XxDeadlyGamerxX Wrote: I'm writing a simple user input
package caleb.fournier;
import java.util.Scanner;
//This line above must be used if you're using user input
public class Caleb
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int age;
System.out.println("How old are you?");
age=in.nextInt();
if (age >= 21);
System.out.println("You are old enough to drink");
else
System.out.println("You are not old enough to drink");
}
}
And getting this error
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error on token "else", assert expected
then cannot be resolved to a variable
at caleb.fournier.Caleb.main(Caleb.java:15)
PLEASE HELP ME.
Try this one :
Code package caleb.fournier;
import java.util.Scanner;
//This line above must be used if you're using user input
public class Caleb
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int age;
System.out.println("How old are you?");
age=in.nextInt();
if (age >= 21)
System.out.println("You are old enough to drink");
else
System.out.println("You are not old enough to drink");
}
}
|