Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about loops and help

 
Eric Johns
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this program that takes user input and searches a data file for cars that meet the users preferences. my program has a general "for" loop that goes throught the whole data file and goes throught every car on the list till it ends, now in addtion to that I have to make another loop ( I think a while loop) that keeps running the program till the user decides to exit. My problem is I can't seem to get it working right. right now I have a string called prompt where the user can input either yes or no if they want to continue with the program. then I have a boolean statement endloop that can be a test for the while loop and a set of if statement to change the endloop to false, but the problem is no matter where it is it doesn't work right if I put it above the while loop statement it will never run the if statement but if I put it below the while loop you have to go thought it again even if you say no before it drops out , is their an easier way I'm not seeing?
sample of what I did.
if(prompt.compareTo("no")==0)
{
endLoop=false;
}
while (endLoop==true)
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See if this lines up with what you're thinking:

Years ago we used to talk about a "primer" operation, one to "prime the pump." It's common to see loops that prime the condition and then test it at the end. Here's one with a "primer read"

Some object to having two reads, but I like how it keeps the loop clean. Here's an alternative that I don't like with only one read but with the condition hidden in the loop body and an ugly break:

That wouldn't work for you, because it would ask the user if they have more before they did the first one!
Hey, beertender! When I edited this message, it added the font size=2 gawp. I was just thinking how nice it was to have larger code font than we used to, but having the HTML tags show up in my msg is not my idea of fun!
[ September 27, 2003: Message edited by: Stan James ]
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan, I suggest that you make that last comment in the JavaRanch Forum since it about the site rather than about this thread/subject.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic