Jump to content

do-while help

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
Root23

Root23

    Programmer

  • Members
  • PipPipPipPip
  • 144 posts
I'm new to Java, and having some problems with a loop. I don't know a lot of things to try w/ my limited knowledge...

Here's a snippet of the code:
do {
carContinue = hw3OP.showInputDialog(null,
            "Do you want to continue?" + "\n" + "Yes - to continue." + "\n" + "No - to quit.", 
            title, hw3OP.QUESTION_MESSAGE);
            
        int lengthCar = carContinue.length();
            if (lengthCar > 2) {
                again = true;
                } else {
                    again = false;
                }
        } while (again = false);

If I need to post all the code just let me know..

Thanks
Posted Image

#2
Root23

Root23

    Programmer

  • Members
  • PipPipPipPip
  • 144 posts
Well after a good bit of searching I found a way to do what I wanted, in the way I was trying originally.. with a few differences.

Instead of a do-while, I switched to just a while loop. I was trying to set the Yes/No dialog as a string, but it needs to be an integer.

Here's a link to the solution I used -> Using JOptionPane Yes/No dialogs to control loops « bushido dev
Posted Image

#3
so1i

so1i

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 312 posts
Yes, in this case it's probably better to use a while loop, as with do loops you always get the code run once, even if the limiting factor is exceeded. Although I don't quite know the context of your code, while will stop the code before it gets run once.
My Company - My Homepage - My Twitter - My Google+ - My LinkedIn

"Things don’t have to change the world to be important.” - Steve Jobs

#4
Root23

Root23

    Programmer

  • Members
  • PipPipPipPip
  • 144 posts
For what I'm doing a do-while would have been fine because I want the code ran once regardless, but both are definitely ok for my assignment.

Originally I had a do-while, and at the end of the 'do' portion I tried to take the YES_OPTION or NO_OPTION and have it stored in a string variable, when it needed to be INT which I didn't know.

Then I tried what I have posted in the first post.. which I still don't understand why it doesn't work.. but the link above ended up working perfrectly, and cleared up some confusion I had with the yes/no option.
Posted Image