Welcome to Dream.In.Code
Become a Java Expert!

Join 150,386 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,101 people online right now. Registration is fast and FREE... Join Now!




Need a loop allowing for repeated user input until exit

 
Reply to this topicStart new topic

Need a loop allowing for repeated user input until exit

Reason4
23 Jan, 2008 - 10:17 AM
Post #1

New D.I.C Head
*

Joined: 20 Dec, 2007
Posts: 29

Folks, when I set up the following code my program will compile. However, it hangs after the first user choice and will not run past that point.

Pseudocode:

Menu offers choice of options 1, 2, 3, or 4.

Program prompts user to enter 1, 2, 3, or 4.

Input processed as follows:

While option >= 1 && option <5;
{
If option = 1:
System.out “You have chosen Option 1.”

If option = 2:
System.out “You have chosen Option 2.”

If option = 3:
System.out “You have chosen Option 3.”

If option = 4:
Exit Program
}

/Pseudocode:

My question is, why is the loop not working? Does anyone have any suggestions on what to use to make the loop functional?

PS: This is not a current homework assignment. It is part of a program I submitted in a class that I have finished. I just want to get to the bottom of the code.

User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Need A Loop Allowing For Repeated User Input Until Exit
23 Jan, 2008 - 10:41 AM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
I don't understand why the loop doesn't work because I don't understand what your code is suppose to do. Please explain better.
User is offlineProfile CardPM
+Quote Post

ajwsurfer
RE: Need A Loop Allowing For Repeated User Input Until Exit
23 Jan, 2008 - 11:26 AM
Post #3

D.I.C Regular
Group Icon

Joined: 24 Oct, 2006
Posts: 298



Thanked: 2 times
Dream Kudos: 50
My Contributions
I'm surprised you got Pseudocode to compile, he, he smile.gif

How come this won't compile in Java?

CODE


print  "Hello World!"


User is offlineProfile CardPM
+Quote Post

Reason4
RE: Need A Loop Allowing For Repeated User Input Until Exit
23 Jan, 2008 - 07:31 PM
Post #4

New D.I.C Head
*

Joined: 20 Dec, 2007
Posts: 29

What I want the program to do is offer a user four options. Three of those options will generate a message when the user picks them. The fourth choice exits the program.

My problem is that all loop examples I have seen involve simple countdowns of some kind, liks this:

CODE


for (i = 1; i<6; i++) {
    System.out.println (i);



But what do you do when you don't want a straight run through the loop? I want a user to be able to keep entering choices 1 through 3 until they get tired of it, then exit by choosing 4. Right now the best I can get is one choice by the user, then the program ends. It is not stepping through the loop for some reason.

I wonder if a DO WHILE loop would work?

I have tried using SWITCH and the program still hangs.

PS: Thanks for your willingness to help -- I know I don't give the best explanations in the world. smile.gif

QUOTE(PennyBoki @ 23 Jan, 2008 - 11:41 AM) *

I don't understand why the loop doesn't work because I don't understand what your code is suppose to do. Please explain better.


User is offlineProfile CardPM
+Quote Post

Reason4
RE: Need A Loop Allowing For Repeated User Input Until Exit
23 Jan, 2008 - 07:40 PM
Post #5

New D.I.C Head
*

Joined: 20 Dec, 2007
Posts: 29

It looks like this is again getting back to my trouble with explanations. I am not asking for precise coding as an answer. I would like to know whether I am approaching the program logic correctly. I have tried (with regular Java-type code) using WHILE, IF, and SWITCH statements. I have not yet tried DO WHILE, but still think maybe SWITCH is the way to go. Or is there something else entirely I am missing? Should I try and nest each potential choice?

Anyway, all the above is why I used pseudocode. Sorry.

QUOTE(ajwsurfer @ 23 Jan, 2008 - 12:26 PM) *

I'm surprised you got Pseudocode to compile, he, he smile.gif

How come this won't compile in Java?

CODE


print  "Hello World!"




User is offlineProfile CardPM
+Quote Post

capty99
RE: Need A Loop Allowing For Repeated User Input Until Exit
23 Jan, 2008 - 08:19 PM
Post #6

the real kya
Group Icon

Joined: 26 Apr, 2001
Posts: 9,259



Thanked: 16 times
Dream Kudos: 550
My Contributions
you need to have the part where you ask the user for a number inside your loop.

so either do a while loop where you set the variable initally outside the loop. (just fib it and say userNum = 2 for example), and then ask in the loop while(userNum >= 1 etc....

or do a do while .

just make sure the prompt is in the loop.



i have a working version of your code if you need help , but its a simple fix.

This post has been edited by capty99: 23 Jan, 2008 - 08:17 PM
User is offlineProfile CardPM
+Quote Post

Reason4
RE: Need A Loop Allowing For Repeated User Input Until Exit
24 Jan, 2008 - 07:36 AM
Post #7

New D.I.C Head
*

Joined: 20 Dec, 2007
Posts: 29

I will give it a try before asking for the "cheat sheet" biggrin.gif

And thank you so much for the tip! That had not occurred to me -- obviously! blink.gif

QUOTE(capty99 @ 23 Jan, 2008 - 09:19 PM) *

you need to have the part where you ask the user for a number inside your loop.

so either do a while loop where you set the variable initally outside the loop. (just fib it and say userNum = 2 for example), and then ask in the loop while(userNum >= 1 etc....

or do a do while .

just make sure the prompt is in the loop.



i have a working version of your code if you need help , but its a simple fix.


User is offlineProfile CardPM
+Quote Post

Reason4
RE: Need A Loop Allowing For Repeated User Input Until Exit
25 Jan, 2008 - 01:38 PM
Post #8

New D.I.C Head
*

Joined: 20 Dec, 2007
Posts: 29

Okay, if you have the working code, I will take it now... sigh.

QUOTE(capty99 @ 23 Jan, 2008 - 09:19 PM) *

you need to have the part where you ask the user for a number inside your loop.

so either do a while loop where you set the variable initally outside the loop. (just fib it and say userNum = 2 for example), and then ask in the loop while(userNum >= 1 etc....

or do a do while .

just make sure the prompt is in the loop.



i have a working version of your code if you need help , but its a simple fix.


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 05:02PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month