Question re keeping a program running

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sosoatjava
    New Member
    • Mar 2007
    • 4

    #1

    Question re keeping a program running

    How can a simple program be kept running until a defined control character, say "x" or menu choice 4 is entered? I can prompt a menu, offer choices, take keyboard input, perform a calculation but would like it to keep "alive" re-prompting the choices again for more input and so on until my control character or number is entered, then to quit. Is it as simple as a while loop and while != "x"?
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Originally posted by sosoatjava
    Is it as simple as a while loop and while != "x"?
    Essentially, yes. I'd suggest a do...while loop (since I assume you want your program to prompt with the menu at least once), but that's preference and won't affect the overall functionality. Also, you can't say "while != "x"", though I'm sure you knew that - you'll need a control variable, like a char.

    Comment

    • sosoatjava
      New Member
      • Mar 2007
      • 4

      #3
      Thanks for that- I can get the "engine" of a problem OK- calculations, etc but the structure is still something I need work on. I would also like to "filter" on anything other than my set options, ie 1, 2, or 3, I would try treating the input as a string, then while input== 1 &&2&&3 route to my various methods; if not, echoing out a warning message to the effect that "please pick 1, 2 or 3"

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        OK, OK. All this is do-able. Now, using a string, you'd have to use the .equals() method rather than ==, and you'd have to test for each possibility (of 1, 2, or 3) separately, but all of this is just technical advice.

        Basically, for input validation (making sure the user enters a proper value), you need a loop. The loop will tell the user that they made a mistake, and remind them of what information they are entering. This loop should continue executing until the information entered is valid.

        Comment

        Working...