maurice.tracey.7 0 Newbie Poster

hi thier i was wondering if you guys could hep me when i press equall it calculates but when i press equall again it adds
the first number added, for example if i was to enter 3 + 2 = 5 if i press enter again it equalls to 8 instead of 7

import java.awt.*; // Use the awt library
import java.awt.event.*;
import java.awt.geom.*;

import javax.swing.*; // Use the swing library

import java.util.Random; // Get random numbers
import java.util.Scanner;
import java.lang.Number.*; // Need this for Integer

import java.lang.Math.*;

// The Moving Component class is a subclass of JComponent and defines an object that listens
// for actions (in this case multiple button presses).
public class CalcComponent extends JComponent implements ActionListener {
    double a;
    double totall;
    double savethis = 0;
    boolean add = false;
    boolean subtract = false;
    boolean divide = false;
    boolean multi = false;
    boolean sinez = false;
    boolean squareroot = false;
    double result = 0;
    JButton buttons[];
    JTextField rField;

    Random randomGenerator = new Random(); // Need this to generate random
                                            // numbers.
    // Set up buttons and add a listener for each. The first four buttons
    // (should)
    // implement some arithmetic operations, the last resets the calculator.

    public CalcComponent() 
    {

        rField = new JTextField();
        add(rField);

        rField.setHorizontalAlignment(JTextField.RIGHT);

        // Send values to the textfields
        display();// 0,0 values

        // GridLayout to lay the interface out in two columns.
        setLayout(new GridLayout(0, 2));
        // Create buttons and add them
        buttons = new JButton[10];
        buttons[0] = new JButton("+");
        buttons[1] = new JButton("-");
        buttons[2] = new JButton("*");
        buttons[3] = new JButton("/");
        buttons[4] = new JButton("Clear");
        buttons[5] = new JButton("Sin");
        buttons[6] = new JButton("Sqrt");
        buttons[7] = new JButton("Max");
        buttons[8] = new JButton("Min");
        buttons[9] = new JButton("=");
        for (int i = 0; i < 10; i++) {
            add(buttons[i]);
            buttons[i].addActionListener(this);
        }

    }

    // Do the relevant operation on the numbers when a function key is
    // hit.
    public void actionPerformed(ActionEvent e) 
    {
        // First, read the value of a from the textfield:
        String aString = rField.getText();
        Double adouble = new Double(aString);
        a = adouble.doubleValue();

        // Now respond to buttons
        // The first button is +, so add up the numbers

        if (e.getSource() == buttons[0])// addition
        {

            totall = Double.parseDouble(rField.getText());
            rField.setText("");
            //addUp(totall);
            add = true;
        }

        if (e.getSource() == buttons[1])// subtraction
        {
            subtract = true;
            totall = totall + a;
            rField.setText("");
            minus(totall);

        }

        if (e.getSource() == buttons[3])// division
        {
            divide = true;
            totall = totall + a;
            rField.setText("");
            division(totall);
        }

        if (e.getSource() == buttons[2])// multiplication
        {
            multi = true;
            totall = totall + a;
            rField.setText("");
            multiply(totall);
        }

        if (e.getSource() == buttons[5])// Sine
        {
            sinez = true;
            totall = a;
            sine(totall);
            display();
        }

        if (e.getSource() == buttons[6])// squareRoot
        {
            squareroot = true;
            totall = a;
            sqrtroot(totall);
            display();
        }
        // This is the Again! button, so reset a, b and result.
        if (e.getSource() == buttons[4]) 
        {
            totall = 0;
            result = 0;
            rField.setText("");
        }

        if(e.getSource() == buttons[7])
        {

            String[] result = aString.split(",");
            double c = Double.parseDouble(result[0]);
            double d = Double.parseDouble(result[0]);
            maximum(c, result[0].hashCode());
            display();
        }
        if (e.getSource() == buttons[9] && add == true)// Addition
        {
            result = Double.parseDouble(rField.getText());
            result = result + totall;
            rField.setText(Double.toString(result));
            display();
        }
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.