Home > Software engineering >  Calculator Clear button functionality isn't working as desired. set to 0 causes multiplication
Calculator Clear button functionality isn't working as desired. set to 0 causes multiplication

Time:01-30

//*****

I stripped down as much as I thought possible while still being pleasing to use. this is my beginner calulator project. it shows result as I type. I'm using multiple textFields to achieve this. I'm open to smarter ways. for now though I would like to lie in my bed as I've made it to learn.

On fresh start up no issues with parsing and the getting a result but after clear lets say I used addButton last it will automatically add the first new number to itself then give me the result as the new num1. if I set num1 to 0; add works but not multiplication because of zero... is .setText(" "); really deleting the values? is there a better way? I've read all I could find on textFields.

I will happily take direction to the correct reading material in lieu of a direct answer. textField numbered slightly out of order. My apologies order is top down - 3 -2 -1 -4 -5. 4 and 5 are to help me visualize the problem 3 shows the work, 2 the result and 1 is used for parsing num1 and setting the current result to num1 so I can chain the addition. I've tried parsing at different locations setting num1 to 0 setting result to the remainder of result and num1 to be the new num2.. and a bunch of other silly attempts at similar solulutions. I know that my if(numberButtons[i]) { statement directly above the switch for - * / where it sets num2=Double.parseDouble(textField4.getText()); is the main cause but it is also the only way I know to give constant results as I type in my numbers. I,ve tried more thought-out and many more arbitrary changes to my code trying to understand what's really going on and what's possible. Afer 3 weeks I am embarrassed for myself. Thank you for your time. Again even just the right direction will help me very much. I do like and desire to solve the problem on my own. I am just truly out of my depth as I am 3 months into this a few hours a day in my time off of work. I truly enjoy it and would love to have a sense of closure and completion to this problem.
///*************

package test;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.border.SoftBevelBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Calc1 implements ActionListener {
    
    JFrame frame;
    JTextField textField3,textField2,textField1,textField4,textField5; 
    JButton[] numberButtons = new JButton[10];
    JButton[] functionButtons = new JButton[6];
    JButton addButton,subButton,multiButton,diviButton,decimalButton;
    JButton equalButton,deleteButton,clearButton,negativeButton;
    JPanel panel;
    JLabel label;
    Font myFont = new Font("Monospaced", Font.BOLD,31);
    Font font1 = new Font("Monospaced", Font.BOLD, 30);
    Font font2 = new Font("Monospaced", Font.BOLD, 28);
    Font font3 = new Font("SansSerif", Font.BOLD, 48);
    Font font4 = new Font("Monospaced", Font.BOLD, 65);
    double num1=0,num2=0,result=0;
    char operator;

    Calc1(){
        Border border1 = new LineBorder(Color.DARK_GRAY, 4, true);
        frame = new JFrame("Calculator");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(null);
        frame.setOpacity(1);
        frame.setVisible(true); 
        frame.setBounds(201, 60, 323, 700);
                
        label = new JLabel();
        label.setBounds(5, 4, 300, 52);
        label.setFont(font1);
        label.setVisible(true);
        label.setText("Calculator");
        label.setForeground(Color.blue);
        label.setFont(new Font("Console", 1, 17));
        label.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
        
        Border border = new LineBorder(Color.LIGHT_GRAY, 7, true);
        textField3 = new JTextField(); 
        textField3.setBounds(5,54, 299, 38);
        textField3.setFont(font2);
        textField3.setBackground(Color.lightGray);
        textField3.setBorder(border);
        textField3.setOpaque(true);
        textField3.setHorizontalAlignment(JTextField.RIGHT);
        textField3.setFocusable(true);
    
        textField2 = new JTextField(); 
        textField2.setBounds(5, 89, 299, 44); 
        textField2.setFont(myFont);
        textField2.setBackground(Color.lightGray);
        textField2.setBorder(border);
        textField2.setVisible(true);
        textField2.setOpaque(true);
        textField2.setHorizontalAlignment(JTextField.RIGHT);
        textField2.setFocusable(false);
        
        textField1 = new JTextField(); 
        textField1.setBounds(5,510, 300, 35); 
        textField1.setFont(myFont);
        textField1.setVisible(true);
    
        textField4 = new JTextField(); 
        textField4.setBounds(5,549, 300, 35); 
        textField4.setFont(myFont);
        textField4.setVisible(true);
        
        textField5 = new JTextField(); 
        textField5.setBounds(5,580, 300, 35); 
        textField5.setFont(myFont);
        textField5.setVisible(true);
            
        addButton = new JButton(" ");
        subButton = new JButton("-");
        multiButton = new JButton("x");
        diviButton = new JButton("÷");
        decimalButton = new JButton(".");
        equalButton = new JButton("=");
        deleteButton = new JButton("←");
        clearButton = new JButton("AC");

        functionButtons[0] = addButton; 
        functionButtons[1] = subButton;
        functionButtons[2] = multiButton;
        functionButtons[3] = diviButton;
        functionButtons[4] = decimalButton;
        functionButtons[5] = clearButton;
        for(int i=0; i<6; i  ) {
            functionButtons[i].addActionListener(this);
            functionButtons[i].setFont(myFont);
            functionButtons[i].setFocusable(false);
            functionButtons[i].setBorder(new SoftBevelBorder(BevelBorder.RAISED));  
        }   
        for(int i=0; i<10; i  ) {
            numberButtons[i] = new JButton(String.valueOf(i));
            numberButtons[i].addActionListener(this);
            numberButtons[i].setFont(font2);
            numberButtons[i].setFocusable(false);
        }   
        panel = new JPanel(); 
        panel.setLayout(new GridLayout(4,4,4,4));   
        panel.add(label);
        panel.setBackground(Color.DARK_GRAY);
        panel.setForeground(Color.BLACK);
        panel.setVisible(true);
        panel.setOpaque(true);
        panel.setBorder(border1);
        panel.setBounds(4, 133, 300, 300);
        
        panel.add(numberButtons[7]);
        panel.add(numberButtons[8]);
        panel.add(numberButtons[9]);
        panel.add(addButton);
        panel.add(numberButtons[4]);
        panel.add(numberButtons[5]);
        panel.add(numberButtons[6]);
        panel.add(subButton);
        panel.add(numberButtons[1]);
        panel.add(numberButtons[2]);
        panel.add(numberButtons[3]);
        panel.add(multiButton);
        panel.add(decimalButton);
        panel.add(numberButtons[0]);
        panel.add(clearButton);
        panel.add(diviButton);
        frame.add(panel); 
        frame.add(label);
        frame.add(textField3);
        frame.add(textField2); 
        frame.add(textField1);
        frame.add(textField4);
        frame.add(textField5);
        
        frame.setOpacity(1);
        frame.setForeground(Color.BLACK);
        frame.setBackground(Color.BLACK);
        frame.setVisible(true);
        
        label.setForeground(Color.blue);
        label.setBackground(Color.GRAY);
        label.setOpaque(true);  
    }
    public static void main(String[] args) {
        Calc1 calc = new Calc1();   
    }
    public  void actionPerformed(ActionEvent e) {
        for(int i=0; i<10; i  ) { 
            if(e.getSource() == numberButtons[i]) { 
                textField1.setText(textField1.getText().concat(String.valueOf((i))));
                textField3.setText(textField3.getText().concat(String.valueOf((i))));   
                textField4.setText(textField4.getText().concat(String.valueOf((i))));   
                textField5.setText(textField5.getText().concat(String.valueOf((i))));
            }
        }
            if (e.getSource()==subButton) {     
                num1=Double.parseDouble(textField1.getText());
                operator='-';
                textField3.setText(textField3.getText().concat("-"));
                textField4.setText("");
                textField4.setText(textField4.getText().concat(""));    
         }
            if (e.getSource()==addButton) { 
                num1 = Double.parseDouble(textField1.getText());
                operator = ' ';
                textField3.setText(textField3.getText().concat(" "));
                textField4.setText("");
                textField4.setText(textField4.getText().concat(""));
         }
            if (e.getSource()==multiButton) {
                num1 = Double.parseDouble(textField1.getText());
                operator = '*';
                textField3.setText(textField3.getText().concat("x"));
                textField4.setText("");
                textField4.setText(textField4.getText().concat(""));    
        }
            if (e.getSource()==diviButton) {
                num1 = Double.parseDouble(textField1.getText());
                operator = '/';
                textField3.setText(textField3.getText().concat("÷"));
                textField4.setText("");
                textField4.setText(textField4.getText().concat(""));
        }   
            for(int i=0; i<10; i  ) { 
                if(e.getSource()==numberButtons[i]) {   
                    num2=Double.parseDouble(textField4.getText());
                
                    switch(operator) {
                    case'-':    
                        result=num1-num2;
                        textField1.setText(String.valueOf(result)); 
                        textField5.setText(String.valueOf(num1));   
                        break;
                    case' ':        
                        result=num1 num2;       
                        textField1.setText(String.valueOf(result));
                        textField5.setText(String.valueOf(num1));       
                        break;
                    case'*':
                        result=num1*num2;   
                        textField1.setText(String.valueOf(result));
                        textField5.setText(String.valueOf(num1));   
                        break;
                    case'/':
                        result=num1/num2; 
                        textField1.setText(String.valueOf(result)); 
                        textField5.setText(String.valueOf(num1));   
                        break;  
                     }
                }       
            textField2.setText(String.valueOf((result)));       
        }
        if (e.getSource()==clearButton) {   
            textField3.setText("");
            textField2.setText("");
            textField1.setText("");
            textField4.setText("");
            textField5.setText(""); 
        }       
    }   
}

CodePudding user response:

Where you clear all your JTextFields, You should also reset all member variables used in previous calculations. After all, beacause of their scope (class global), they will still be holding values unless reset:

if (e.getSource() == clearButton) {   
    textField3.setText("");
    textField2.setText("");
    textField1.setText("");
    textField4.setText("");
    textField5.setText(""); 
    num1 = 0;
    num2 = 0;
    result = 0;
    operator = '\0';
}     
  •  Tags:  
  • Related