Java Help: String Checking

I'm finishing up a project due by midnight today.  I'm supposed to make a Guessing game using a bit of object-oriented programming.  I've got it pretty much done, except for one part.

I need to make the program loop if you want to do a replay.  The example here in the assignment says:

Play again? Y/N: 

you input Y or N if you want to replay, or don't want to replay.  We're assuming there is no human error here.  I tried to create a large while loop around the game program, like this:

(this is the runner class, handles user input)

int rLoop = 0;

while (rLoop = 0) {

-game stuff here-

rLoop = n.replay(replay);

}

(this is the Game class, handles aspect of the game)

int replay(String Replay) {

if (Replay == "Y") {

return 0;

} else {

return 1;

}

}

It quits whenever I put Y or N.  Doesn't matter what I put, really.  I need to figure out how to make the code recognize if i put Y or N, and change an integer according to it to stop/continue the loop.

Ok first, Shouldn't the while loop condition at the top be rLoop == 0 instead rLoop =0. And also the mistake your making is that you can never compare two String directly with a relational operator. You are required to use some sort of method from the String class.In this case you would use the equals method which compares two string and determines if they are equal by returning a boolean value. 

I forgot to put the ==, but it's there in my code.

I remember reading about == with strings, now.  I'll see if i can find the equals method.

don't do == with strings, always use .equals(), for example:

String s1 = "example";

String s2 = "example";

boolean b = s1.equals(s2);

so your function would look like this:

int replay(String Replay) {

if (Replay.equals("Y") {

return 0;

} else {

return 1;

}

since you're only using 0 or 1 i'd advice to make the function return a boolean and to change your 'rLoop' variable into a boolean, so your game would look like this

boolean rLoop = true;

while (rLoop) {

-game stuff here-

rLoop = n.replay(replay);

}

and the function would look like this:

boolean replay(String Replay) {

return Replay.equals("Y");

}

 

equals() is a method of the Object class, the String class overrides it.

== tests object reference

equals() tests object equivalence

Make sense?

I recommend equalsIgnoreCase() since this is user input

true, but since OP said there'd be no human error i didn't think to take it into account

Thanks for all the help guys.  Since the project states that all the input is "clean", I don't need to program any anti-error stuff :3.

I went with:

if (Replay.equals("Y")) {

return 0;

}

works fine now!

I think it would be more proper to use something like this:

boolean gameOver = false;

while (!gameOver) { 

for the game stuff and check for input.

if (!Replay.equals("Y")) { 

gameOver = true;

}

 

 

I suspect the teacher was really looking for a do { ... } while (...); loop. Of course other ways will work too, but the point is to have the test at the end, where it most makes sense.

P.S. This guy's assignment was due 4 weeks ago...