Hi everyone,

I have a javascript, jQuery, and c++ background so im not completely new but am just starting to learn Java in a University setting.

In our first program we are to run it within a Do...While loop so the user can click a JButton for yes or no, if they wish to run it again. It's a simple Prime Factorization program. I missed a day this week and think I missed the instruction I am missing for this loop to work. From what I have been reading for the last few hours is that I need to have an event handler to catch the click and feed that to the while condition. I can't find one single example of a do...while loop within java that works off of a user click. Can someone give me an example of what the actionPerformed should pass to the while condition to loop back through the program or point me to a link that has an example? The rest of the program will be easy to write, but this simple thing has me stumped. Seems like an awful lot of work to make a simple do while loop, so I must be missing something.

Thanks,

Curtis

Recommended Answers

All 10 Replies

what the actionPerformed should pass to the while

Can you write a list of the steps the program should take to do what you want?
The normal GUI program shows the user a form and waits for the user's input. When the user clicks on a button the action listener method is called and it does some processing, perhaps prepares data and shows it to the user and exits to wait for the user to do more inputting.
Not sure where the loop you are talking about fits in this scheme.
The "loop" in the processing I described above would be between the display of the GUI, the action listener and then back to the display of the GUI.

start your main with a do while loop, get user input (ints), do some calculating, output to gui and ask user if they want to calculate more with two buttons, yes or no. if yes is clicked the program starts over. in c++ we just put a do inside the main and the while at the end of the main, check condition to go back to the do or exit. it seems that java makes u use action events and im not versed in them or the syntax to use for them with an event handler, etc.

Did you understand what I meant by the "loop" in the program's processing?
Your code would not use a do while loop to wait for user input. Your code does a little bit, then exits and awaits an event from the user. When it gets an event, it does a little and then exits and waits for the next event.

Sorry, I missed that line somehow. That makes sense. In my mind I was thinking that it would be structured like I am used to, 'do' at beginning of main and 'while' at end of main with a condition like while(answer == 1); etc. But I couldn't understand how to interpret the onClick. Does the onClick just output a boolean that can be captured? I have been reading so much crap and everything seems to skirt around actually teaching how to do a simple event handler, including our text book. Even the do while loops they show are the same I did with C++. Thanks for the input, ill keep trying.

basically, I just simply want to rerun the program from the beginning if the user clicks YES, meaning they do want to run it again versus NO, exit. We were told to use a do while loop, but I didn't realize it involved a ton of other stuff to run a simple loop like that.

Running the do while is one thing.
Using GUI to get input from a user is something else.

If you used a loop, what would the code do when it is waiting for user input?
With the event driven method, your code is not executing at all. You use what the user inputs to control what code to execute.

Thanks Norm, maybe I am thinking of it wrong. I will do some more reading and see if I can get this thing to make more sense.

There are two fundamentally different approaches to this:
1. You run the program, then display the yes/no buttons. When the user clicks the "yes" button your event handler runs the program again. When he clicks "no" you exit. This approach doe NOT have an explicit loop
2. You start a loop. In it you run the program then you display a MODAL yes/no dialog which waits for the user to click a button. When a button is clicked you continue your loop or exit depending on which button. This approach does not have an event handler in your code (there is one, but it is handled within the modal dialog itself).
If your requirement is to use a loop, then approach 2 seems appropriate. Look at the showConfirmDialog(...) method in the JOptionPane class

awesome, I think being that our class has just started in the last couple of weeks and we haven't covered event handling she may want the second option. Wish I hadn't missed that damn class Tuesday. Trying to get notes from someone still. Thanks a ton, that really helps a lot. I will google that method and am almost certain that is the way she wants us to do it so she can later show us more advanced ways to solve this.

Curtis

That worked perfectly without having to code a ton of other stuff to just repeat the program from the beginning. here is the basic stuff just to get it working for now, the guts I will start coding now.

/*Curtis Adams - [email]cadams927@gmail.com[/email]
Java I - B Johnston
Program 1 - Prime Factors Main*/

import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.*;

public class PrimeFactorsMain extends JFrame
{
    public static void main(String[] args)
        {
            int jInput;
            do{

            String output = "\nCurtis Adams - Java I\nProgram 1 - Prime Factors\n\n";
            JOptionPane.showMessageDialog(null, output);





            jInput = JOptionPane.showConfirmDialog(null,"YES_NO_OPTION",
               "YES_NO_OPTION", JOptionPane.YES_NO_OPTION);

            }while(jInput == JOptionPane.YES_OPTION);

        }
}

Thanks you two, much appreciated.

Curtis

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.