hi. i got some problems with the dot and the C in th calculator i'm making.
1. when i press the C button it clears the screen but the next number i enter is also ignored even though it appears on the screen.
2. the dot is not working as it should for example if i multiply 0.1 * 0.1 = 0.010000001 or if i add multiple times 0.1 at the 8 time i get 0.70000005 or if substracting i get -0.70000005
i would appreciat some help.
thanks in advance.

    import java.applet.Applet;
    import java.awt.BorderLayout;
    import java.awt.Button;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.GridLayout;
    import java.awt.Label;
    import java.awt.Panel;
    import java.awt.TextArea;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    public class AppletCalculadora extends Applet implements ActionListener {
        Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, c1, c2, c3, c4, c5, c6, c7,
                c8, c9;
        Panel p1, p2;
        Label l1;
        TextArea t1;
        boolean start = true;
        boolean firstDigit = true;
        boolean pressed = false;
        float previousValue = 0;
        String operator = "=";

        public AppletCalculadora() {
            setLayout(new BorderLayout());
            p1 = new Panel(new GridLayout(6, 3));
            p2 = new Panel(new BorderLayout());
            b1 = new Button("1");
            b2 = new Button("2");
            b3 = new Button("3");
            b4 = new Button("4");
            b5 = new Button("5");
            b6 = new Button("6");
            b7 = new Button("7");
            b8 = new Button("8");
            b9 = new Button("9");
            b0 = new Button("0");
            c1 = new Button(".");
            c2 = new Button("CE");
            c3 = new Button("+");
            c4 = new Button("一");
            c5 = new Button("=");
            c6 = new Button("*");
            c7 = new Button("/");
            c8 = new Button("C");
            // l1 = new Label("");
            Dimension d = p2.getSize();
            t1 = new TextArea("0", d.width, d.height, TextArea.SCROLLBARS_NONE);
            p1.add(b1);
            p1.add(b2);
            p1.add(b3);
            p1.add(b4);
            p1.add(b5);
            p1.add(b6);
            p1.add(b7);
            p1.add(b8);
            p1.add(b9);
            p1.add(c1);
            p1.add(b0);
            p1.add(c2);
            p1.add(c3);
            p1.add(c4);
            p1.add(c5);
            p1.add(c6);
            p1.add(c7);
            p1.add(c8);
            p2.add(t1, BorderLayout.CENTER);
            p2.setBackground(Color.white);
            p2.setFont(new Font("Serif", Font.BOLD, 16));
            add(p1, BorderLayout.SOUTH);
            add(p2, BorderLayout.NORTH);
            b1.addActionListener(this);
            b2.addActionListener(this);
            b3.addActionListener(this);
            b4.addActionListener(this);
            b5.addActionListener(this);
            b6.addActionListener(this);
            b7.addActionListener(this);
            b8.addActionListener(this);
            b9.addActionListener(this);
            b0.addActionListener(this);
            c1.addActionListener(this);
            c2.addActionListener(this);
            c3.addActionListener(this);
            c4.addActionListener(this);
            c5.addActionListener(this);
            c6.addActionListener(this);
            c7.addActionListener(this);
            c8.addActionListener(this);

        }

        public void actionPerformed(ActionEvent ae) {

            String input = ae.getActionCommand();

            if (input.charAt(0) >= '0' && input.charAt(0) <= '9') {
                if (start) {
                    t1.setText(input);
                    start = false;
                } else {
                    t1.setText(t1.getText() + input);
                }

            } else if (input.equals(".")) {              // got problem here with the dot
                if (!start && !pressed) {
                    t1.setText(t1.getText() + input);
                    pressed = true;

                } else if (start && !pressed) {          
                    t1.setText("0.");
                    start = false;
                    pressed = true;
                    showStatus("pressed start" + t1.getText() + pressed);
                }

            } else if (input.equals("CE")) {

                t1.setText("0");
                start = true;

            } else if (input.equals("C")) {        // also got problems here or in the switch case "C"

                // t1.setText("0");
                operator = "C";
                calculate("0");
                start = true;

            }

            else {
                if (!start) {
                    calculate(t1.getText());
                    start = true;
                    pressed = false;
                }

                operator = input;
            }

        }

        public void calculate(String input) {
            float val = Float.parseFloat(input);
            switch (operator) {
            case "=":
                previousValue = val;
                break;
            case "+":
                previousValue += val;
                break;
            case "一":
                previousValue -= val;
                break;
            case "*":
                previousValue *= val;
                break;
            case "/":
                previousValue /= val;
                break;
            case "C":                            // may be the problem is with "C" is here
                previousValue = val = 0;
                break;

            }

            t1.setText("" + previousValue);
            showStatus("" + operator + pressed + previousValue + start);
        }

        public void init() {
            setBackground(Color.cyan);
            setForeground(Color.blue);
            setFont(new Font(null, Font.BOLD, 11));

        }
    }   

already fixed the problem with the button C but still have no idea how to fix the dot problem.

It's almost impossible to read your code, with unhelpful variables names and no comments, but maybe there's a problem with resetting the "pressed" boolean? The only place you reset it is in the else on 133, but given the obscurity of the code I have absolutely no idea under what conditions than block will be executed. At the very least I owuld expect it to be reset when you press CE, but maybe it needs to be reset whenever you press anything other than a digit button?

sorry , i just changed a little the code and the name of the variables,hope now can be comprehensible.
as i said before already fixed the problem with the button "C"
but still can't fix the dot "." button problem.

the dot is not working as it should for example if i multiply 0.1 * 0.1 = 0.010000001 or if i add multiple times 0.1 at the 8 time i get 0.70000005 or if substracting i get -0.70000005

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AppletCalculadora2 extends Applet implements ActionListener {
    Button button[] = new Button[18];
    String buttonOperators[] = { ".", "0", "CE", "十", "一", "=", "*", "/", "C" };
    Panel displayPanel, buttonPanel;
    TextArea display;
    boolean firstDigit = true; // to check if is the first digit to enter
    boolean dotPressed = false;// check if the dot button has been pressed
    float previousValue = 0;
    String operator = "=";

    public void init() {
        setBackground(Color.cyan);
        setForeground(Color.blue);
        setFont(new Font(null, Font.BOLD, 11));

        setLayout(new BorderLayout());
        buttonPanel = new Panel(new GridLayout(6, 3));
        displayPanel = new Panel(new BorderLayout());
        for (int i = 0; i < 9; i++) {
            button[i] = new Button(String.valueOf(i + 1));
            // create buttons 1 to 9
        }

        for (int i = 10; i < 19; i++) {
            button[i - 1] = new Button(buttonOperators[i % 10]);
            // create buttons for 0 and operators

        }
        Dimension d = displayPanel.getSize();
        display = new TextArea("0", d.width, d.height, TextArea.SCROLLBARS_NONE);

        for (int i = 0; i < 18; i++) {
            buttonPanel.add(button[i]);
        }
        displayPanel.add(display, BorderLayout.CENTER);
        displayPanel.setBackground(Color.white);
        displayPanel.setFont(new Font("Serif", Font.BOLD, 16));
        add(buttonPanel, BorderLayout.SOUTH);
        add(displayPanel, BorderLayout.NORTH);

        for (int i = 0; i < 18; i++) {
            button[i].addActionListener(this);
        }
    }

    public void actionPerformed(ActionEvent ae) {

        String input = ae.getActionCommand();

        if (input.charAt(0) >= '0' && input.charAt(0) <= '9') {
            if (firstDigit) { // add the first number to the display
                display.setText(input);
                firstDigit = false; // change the boolean first digit to false
            } else {
                display.setText(display.getText() + input);
                // add the other numbers to the display
            }
        } else if (input.equals(".")) { // if the dot is pressed
            if (!firstDigit && !dotPressed) { // add dot to the display
                display.setText(display.getText() + input);
                dotPressed = true;

            } else if (firstDigit && !dotPressed) {
                display.setText("0."); // if is first digit add "0." to the
                                        // display
                firstDigit = false;
                dotPressed = true;
                showStatus("pressed start" + display.getText() + dotPressed);
                // showstatus is just to check if it's working
            }
        } else if (input.equals("CE")) { // clear the screen
            display.setText("0");
            firstDigit = true;

        } else if (input.equals("C")) { // clear the screen and reset the input
            display.setText("0");
            previousValue = 0;
            firstDigit = true;

        } else { // manage operators
            if (!firstDigit) { // if not first digit do operation
                calculate(display.getText());
                firstDigit = true;
                dotPressed = false;
            }
            operator = input; // sets the operator to the input
        }
    }

    public void calculate(String input) {
        float currentvalue = Float.parseFloat(input);
        // set the value taken from the display to a float
        switch (operator) {
        case "=":
            previousValue = currentvalue;
            break;
        case "十":
            previousValue += currentvalue;
            break;
        case "一":
            previousValue -= currentvalue;
            break;
        case "*":
            previousValue *= currentvalue;
            break;
        case "/":
            previousValue /= currentvalue;
            break;
        }
        display.setText("" + previousValue); // display the operation on screen
        showStatus("" + operator + previousValue + dotPressed + firstDigit);
        // just to check changes in operator, booleans and value

    }

}

That looks a lot better!
Apart from not resetting dotPressed after a C or CE I can't see any obvious problem. What exactly are the symptoms now?

ps Manage Operators - you seem to call calculate before you set the variable "operator" - is that right? WHy isn't operator passed as a parameter to that method, like the current value?

as far as i have been checking i don't find problems with C or CE any more.
the problem that i have is that for example if you multiply 0.1 * 0.1 the answer you will get is 0.010000001 instead of 0.01 or if you add 0.1 multiple times at the eight time the answer will be 0.70000005 instead of 0.8 or -0.70000005 if substracting.

i just don't know why it happens or how to fix it.
thanks again

ps.
fixed the dotPressed as suggested but haven't fixed the problem explained above.

else if (input.equals("CE")) { // clear the screen
            display.setText("0");
            firstDigit = true;
            dotPressed = false;

        } else if (input.equals("C")) { // clear the screen and reset the input
            display.setText("0");
            previousValue = 0;
            firstDigit = true;
            dotPressed = false;
            }

Ah ha! That looks like a standard floating point arithmetic rounding problem, nothing to do with your dot.
Decimals like 0.1 don't have an exact equivalent in a binary floating point number, so when you use them in arithmetic you often get an error in the very last digit of the floating point value. You can reduce these problems by using a higher-accuracy floating point type, ie double, but the problem still won't entirely go away.

This link explains the problem in more detail, and shows you how to use the BigDecimal class for exactly correct calculations on numbers with decimals.

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.