Why won't my loop stop looping?

System.out.println("Would you like to play, or just quit right now? (type <quit> or <play> and hit enter)");

String proceed = sc.next();

answer = proceed.equals("play");

if(answer ==true)

{

do

{

play();

}

while(answer ==true);

}

}

publicvoid play()

{

trace("play method");

StarletsWorld londonSheraton;

londonSheraton =new StarletsWorld();

londonSheraton.setTracing(true);

londonSheraton.newGame();

System.out.println("Would you like to play again, or quit now? (type <quit> or <play> and hit enter)");

String proceed = sc.next();

}

this is the relevant code, i have a library class and this is the organiser class. I cannot for the life of me figure out why this loop continues to run, even after the variable answer has been changed to false by the user entering anything other than "play".

can someone please shed some light on why my loop just keeps on going?

[1615 byte] By [Dangermousea] at [2007-11-27 4:56:34]
# 1
I don't see anywhere in your code where answer is set to false. Hence infinite loop.
floundera at 2007-7-12 10:11:47 > top of Java-index,Java Essentials,Java Programming...
# 2

Hi dangerMouse,

From what it looks like to me, your play() method doesn't return anything, and your String proceed in the play() method gets destroyed when the play method ends. So, your program never knows if your user enters anything. Your answer is always true, so the program keeps looping. You'll need to find some way to put the user's answer in the correct scope so the while loop can see it.

Hope that helps,

Jezzica85

jezzica85a at 2007-7-12 10:11:47 > top of Java-index,Java Essentials,Java Programming...
# 3

You set the value of answer exactly once - before the loop. It has the value "true" before the loop starts and it always has that value because you never change it.

Notice that a line likeanswer = proceed.equals("play");

just sets the value of answer. It doesn't "tie" answer to the proceed string in any way.

Also note that in your play() method you declare and initialise another proceed string. This string has nothing whatsoever to do with the proceed string in the code at the start. Local variables like this (variables declared in a method) are thrown away when the method ends.

If you want play() to influence your loop in some way, then you will have to have it return a value.public boolean play() {

// etc

return sc.next().equals("play");

}

pbrockway2a at 2007-7-12 10:11:47 > top of Java-index,Java Essentials,Java Programming...
# 4

thanks, i'll have to have a play around then.

System.out.println("Would you like to play, or just quit right now? (type <quit> or <play> and hit enter)");

String proceed = sc.next();

answer = proceed.equals("play");

if(answer == true)

{

do

{

play();

System.out.println("Would you like to play again, or quit now? (type <quit> or <play> and hit enter)");

String proceed2 = sc.next();

answer = proceed2.equals("play");

}

while(answer == true);

fixed it. thanks for your replies all.

Dangermousea at 2007-7-12 10:11:47 > top of Java-index,Java Essentials,Java Programming...
# 5
Thisif(answer == true)is simplified toif(answer)
floundera at 2007-7-12 10:11:47 > top of Java-index,Java Essentials,Java Programming...
# 6
Possibly the whole if-do-while thing could be written aswhile(answer) {play();// get answer}
pbrockway2a at 2007-7-12 10:11:47 > top of Java-index,Java Essentials,Java Programming...
# 7
thanks again.i'll try to simplify it as much as possible.
Dangermousea at 2007-7-12 10:11:47 > top of Java-index,Java Essentials,Java Programming...