Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How to restart the math in the program without geting out of the program ?

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to restart the math in the program without geting out of the program ?

    Will I know the title is not that clear, but I will explain what is my problem in a clear way.

    I'm working in a GUI program, but I'm not going to put the code because there is a lot of code and files. Instead, I will try to put it an example.

    Let say:

    I'm working in a GUI program that ask form the user to enter two number's. When the user
    press at the calculate button. It will show up the output. However, the program won't exit unless the user
    press at red (X).

    int x = user_Input1;
    int y = user_Input2;
     
    int total = x + y; // 
     
    JOptionPane.showMessageDialog(null, total);

    I know that there will be a (total) now,

    so my question is here how can I reset all the calculation and have a new total that will show up when the user enter two number's again.



  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,786
    My Mood
    Mellow
    Thanks
    175
    Thanked 625 Times in 612 Posts

    Default Re: How to restart the math in the program without geting out of the program ?

    Use a loop of some kind (a while might be best), collecting the user's input each time through the loop, calculating new results which are then displayed. You might try this approach in a similar console application before using the JOptionPanes, but maybe it doesn't matter.

    Come back when you need more help.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Location
    Finland
    Posts
    23
    My Mood
    Bored
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: How to restart the math in the program without geting out of the program ?

    so my question is here how can I reset all the calculation and have a new total that will show up when the user enter two number's again.
    Im not sure if i understand your question correctly. So sorry if my answer misses the point.

    In your example, i understand you have a program with two input fields, one output field and a button that triggers the calculation? And you want to evaluate the input data and change the output field accoding to that?

    If that is correct, i think what you are looking for ActionListener of some type. You need to listen for the button's onClick -event. When it triggers, you read the inputs, calculate the output and update the output field.

    In semi pseudo code style:
    ...add( new Button("calculateButton") { 
    @Override
    public void onClick() {
    int firstInput = getFirstInput(); //implementation of getFirstInput() read the input field and does all the casting and null checks etc..
    int secondInput = getSecondInput();
    int result = calculate(firstInput, secondInput);
     
    updateOutputComponent(result);
     
    }
    );

    The point being that you need to listen for the clicking of the button and let that trigger actual calculation.
    Read more about even listeners:
    How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)

Similar Threads

  1. how to restart my program
    By jack_nutt in forum Java Theory & Questions
    Replies: 15
    Last Post: November 16th, 2011, 06:39 PM
  2. Math program problem.
    By dylanka in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 27th, 2011, 03:44 PM
  3. help w/ PI Math java program
    By robertsbd in forum What's Wrong With My Code?
    Replies: 8
    Last Post: February 16th, 2011, 08:43 PM
  4. Help need on math java program
    By zidangus in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 6th, 2010, 07:41 PM
  5. math quiz program
    By hope.knykcah in forum Collections and Generics
    Replies: 1
    Last Post: October 23rd, 2009, 09:53 AM

Tags for this Thread