aspose file tools*
The moose likes Beginning Java and the fly likes Question about loops and help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about loops and help" Watch "Question about loops and help" New topic
Author

Question about loops and help

Eric Johns
Greenhorn

Joined: Sep 13, 2003
Posts: 20
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

Joined: Jan 29, 2003
Posts: 8791
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 ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12
Stan, I suggest that you make that last comment in the JavaRanch Forum since it about the site rather than about this thread/subject.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about loops and help