![]() |
|
[Java] Java error - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Java] Java error (/showthread.php?tid=3649332) |
Java error - XxDeadlyGamerxX - Jul 26, 2013 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. * - g4143 - Jul 27, 2013 You have a semi-colon here which should not be here.. Code if (age >= 21);* - vitafortis - Jul 27, 2013 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. * - g4143 - Jul 27, 2013 (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. * - vitafortis - Jul 27, 2013 (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 ;) * - g4143 - Jul 27, 2013 (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'* - vitafortis - Jul 27, 2013 (Jul 27, 2013 03:26 AM)g4143 Wrote: Odd, my compiler generated this. 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. * - Kristoff - Jul 27, 2013 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. * - MyLittlePony - Jul 27, 2013 (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: Lul my compiler would throw me an error for that let alone a warning. * - FlamingWind - Jul 27, 2013 (Jul 26, 2013 11:02 PM)XxDeadlyGamerxX Wrote: I'm writing a simple user input Try this one : Code package caleb.fournier; |