View Full Version : dr java

01-17-2012, 10:21 PM
Im trying to make a a program that operates almost like a slot machine and this is what i have so far. im having a bit of trouble with the "if" statments so can someone tell me what i did wrong. also it wont let me add "else if" to the second "if"

import java.io.*;
class SlotGame
public static void main (String[] args) throws IOException
BufferedReader myInput = new BufferedReader (new InputStreamReader(System.in));

String inName;
String inanswer1;
System.out.println ("Please Input Your Name");
inName = myInput.readLine ();

System.out.println (inName + ",Welcome to the Slot Game!");
System.out.println (inName + " Would you like to play?(y/n)");
inanswer1 = myInput.readLine ();
if (inanswer1.equals("y"));
System.out.println ("Lets Play!");
else if (inanswer1.equals("n"))
System.out.println ("GAME OVER");


01-18-2012, 01:26 AM
i don't know if java supports readLine() like C# (I thought it didn't- you have implamented it like C#)

also- and if the way you did it is accurate - then your if statement issues are due to a lone ";"

if (inanswer1.equals("y"));
System.out.println ("Lets Play!");
else if (inanswer1.equals("n"))
System.out.println ("GAME OVER");

Edit: I also did not check to see if the implamentation of inanswer1.equals("") was done correctly- I am only looking @ your if statement and noting the readLine() -bc I thought Java did not support readLine()-

01-18-2012, 05:12 AM
ReadLine exists in Java's buffered reader, and the equals is also fine (comparisons of java strings should only ever be done via methods such as .isempty or .equals, and never with ==).
The suggestion I have is to abandon the bufferedreader and use the scanner instead as its a lot easier to work with:

Scanner myInput = new Scanner(System.in);
inName = myInput.nextLine();