Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Java error

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.
You have a semi-colon here which should not be here..
Code
if (age >= 21);
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 ~
[Image: 9N6Aq1f.gif]

(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 ~
[Image: 9N6Aq1f.gif]

(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 ~
[Image: 9N6Aq1f.gif]

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.

[Image: 5fw8ZJh.png]
(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");

}

}


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Java] by President HolyFeb 8, 2014 11:48 AM 3 193 Feb 8, 2014 11:48 AM
Last Post: Knautschtabak
[C] by A-MANJul 16, 2013 07:20 PM 0 145 Jul 16, 2013 07:20 PM
Last Post: A-MAN
[Java] by CatchMeIfYouCanOct 23, 2012 12:53 PM 6 444 Oct 23, 2012 12:53 PM
Last Post: redpois0n