*
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "loops" Watch "loops" New topic
Author

loops

John Swain
Greenhorn

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
Hi,


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
Greenhorn

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
Bartender

Joined: Oct 02, 2003
Posts: 10925
    
  12

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: http://aspose.com/file-tools
 
subject: loops
 
Similar Threads
I can not pass Assigment 1b (Hundred) Help!!!!!!
Dereferencing and int
how to delete files datewise..
multidimensional array
Enhanced for loop and Iterator question