File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes loops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "loops" Watch "loops" New topic


John Swain

Joined: Feb 12, 2005
Posts: 16
hello, this is a simple question. could some one tell me how to use a loop so that a program will run untill the user enters "no". This would be the outer loop and other calculations would be done in the inner loop. Could i use boolean for this? if not please explain.
Jimmy Die
Ranch Hand

Joined: Nov 20, 2003
Posts: 97

How I see the problem is that once you start a loop that will keep running until someone presses the letters 'n' and then 'o' and then 'enter' at the command prompt is that the loop will be running in single thread. You will have to develope another thread to listen and/or check for input for each iteration.

Boolean could be used depending upon your setup.

Write what you have so far on the post, and someone can help you along.

Jimmy Die
John Swain

Joined: Feb 12, 2005
Posts: 16
ok, I am very new to java so if my code is extremly messed up please dont mind.

this is only partial code. I want to add the outer loop first, which is to get the user to enter a response of yes or no. if yes then the inner loop initiates and does calculations. those are for annual interest, monthly interest, and daily interest within a 10 year period (inner loop).
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11498

there are two possible interpretations when you say "...a program will run until the user enters 'no'"

1) the program asks for some input. the user enters data. the program calculates a result. the program says "do you want to do it again?", and waits for an input

2) the program starts calculating/doing something. maybe drawing a moving image across a screen, maybe calculating the digits of pi, whatever. it's going great guns-a-blazing, using as much cpu time as it can get. the program never explicitly says "should i stop now?" - it just keeps going, until some event, like the user typing 'no' or hits 'ESC'.

these are two very different problems. I think Jimmy was answering the latter problem. if you're just starting java, I hope you're doing the former.

to do it, you could have a do/while loop. this garantees your loop executes at least once. assuming you can get the user's input, you'd then (in your while condition) compare what they input to some literal, e.g. "NO". as long as the string they input is NOT the same, your loop will continue.

there are a few caveats - you need to use the .equals method, not == for the string comparison. if you compare against "NO", what do you want to do if the user enters "no", "No", "N" or "n" (there are other possibilities to consider here as well).

Hopefully this will give you some ideas what to do next, or what questions you need to ask next.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I agree. Here's the link:
subject: loops