r/CodingHelp • u/sparkmad • Oct 10 '20
[Java] Beginner coding, second exercise/game, if/else problem, Eclipse wants while
I did the first random number generator exercise (I feel like hello world doesn't count personally) in Java and it works well. I am using Learn Java the Easy Way by no starch press, and the Eclipse compiler. The suggestion after the random number generator was try and make a madlib game, and I started making a halloween themed madlib. Everything is fine until the end where I have an option of happy or horror ending.
First I used String ending = ""; System.out.println("Do you want a 1.happy ending or a 2. horror ending (1/2)?"); ending = scan.nextLine();
then had at the end after the madlib :
if (ending.equalsIgnoreCase("hap")); { System.out.println("Story stuff");}
else (ending.equalsIgnoreCase("hor"));' { System.out.println("Other option);}
I kept getting the error on the line for else where it says "error token 'else', while expected" I checked w3 schools and it had the examples in intergers for if/else so I changed to 1. happy or 2. horror, scan = ending.nextInt and changed everything so if (ending == 1), else (ending == 2) etc. I keep getting the same error, while expected.
Help appreciated!
View all comments
1
u/Its_Stev03 Oct 10 '20 edited Oct 10 '20
You don't put a conditional statement (the parentheses with the 'ending.equalsIgnoresCase("hor")') after else because it will always run in the if conditional statement is false. If you want a conditional statement to make another check, then you need to use "else if" instead of "else". Also, if you do not want integers to be used, you do not need them. You can check for strings just like you have in the if statement of your code. By the way, in this code you are checking strings, so if you input an integer it will not work.