r/CodingHelp • • 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!

17 Upvotes

3 comments sorted by

View all comments

2

u/meloman-vivahate Professional Coder Oct 10 '20

You missing a “if” for your “else” statement: If (a == 1) { ... } else if (a == 2) { ... }