0

I am creating a java gui calculator. I have created just about everything else for the calculator just having issues on how to collect the users entire number before the math symbol is used and then the second input number. If someone could help me get a start on that I think I can solve the rest. Thanks!

I honestly don't know how I would go around trying to collect the input of the entire number they may enter say maybe 1304 or so, as my buttons only add numbers not digits. Here is my code:


package calculator;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
    import javax.swing.*;
import javax.swing.border.Border;

public class Calculator {
 
    String currentText = "";
    JTextField screen;
  
    JButton button1;
    JButton button2;
    JButton button3;
    JButton button4;
    JButton button5;
    JButton button6;
    JButton button7;
    JButton button8;
    JButton button9;
    
    JButton add;
    JButton subtract;
    JButton multiply;
    JButton divide;
    JButton clr;
    JButton equals;

    JPanel panel;
    JFrame frame;
    
    
    public Calculator(){
        
        frame = new JFrame("calculator");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(100, 100);
        frame.setSize(300, 425);
        Border border = BorderFactory.createLineBorder(Color.black,1);
        
        //set up panel
        panel = new JPanel();
        panel.setLayout(null);
        
        
        
        screen = new JTextField("0");
        screen.setLocation(25,25);
        screen.setSize(175,50);
        screen.setBackground(Color.white);
        screen.setOpaque(true);
    
        
        button1 = new JButton("1");
        button1.setLocation(25,100);
        button1.setSize(50,50);
        button1.addActionListener(new Click());
        button1.setActionCommand("Action1");
        
        button2 = new JButton("2");
        button2.setLocation(25,155);
        button2.setSize(50,50);
        button2.addActionListener(new Click());
        button2.setActionCommand("Action2");
        
        button3 = new JButton("3");
        button3.setLocation(25,210);
        button3.setSize(50,50);
        button3.addActionListener(new Click());
        button3.setActionCommand("Action3");
        
        button4 = new JButton("4");
        button4.setLocation(25,265);
        button4.setSize(50,50);
        button4.addActionListener(new Click());
        button4.setActionCommand("Action4");
        
        button5 = new JButton("5");
        button5.setLocation(80,100);
        button5.setSize(50,50);
        button5.addActionListener(new Click());
        button5.setActionCommand("Action5");
        
        button6 = new JButton("6");
        button6.setLocation(80,155);
        button6.setSize(50,50);
        button6.addActionListener(new Click());
        button6.setActionCommand("Action6");
        
        button7 = new JButton("7");
        button7.setLocation(80,210);
        button7.setSize(50,50);
        button7.addActionListener(new Click());
        button7.setActionCommand("Action7");
        
        button8 = new JButton("8");
        button8.setLocation(80,265);
        button8.setSize(50,50);
        button8.addActionListener(new Click());
        button8.setActionCommand("Action8");
        
        button9 = new JButton("9");
        button9.setLocation(50,320);
        button9.setSize(50,50);
        button9.addActionListener(new Click());
        button9.setActionCommand("Action9");
        
        add = new JButton("+");
        add.setLocation(150,100);
        add.setSize(50,50);
        add.addActionListener(new Click());
        add.setActionCommand("+");
        
        subtract = new JButton("-");
        subtract.setLocation(150,155);
        subtract.setSize(50,50);
        subtract.addActionListener(new Click());
        subtract.setActionCommand("-");
        
        multiply = new JButton("*");
        multiply.setLocation(150, 210);
        multiply.setSize(50,50);
        multiply.addActionListener(new Click());
        multiply.setActionCommand("*");
        
        divide = new JButton("÷");
        divide.setLocation(150,265);
        divide.setSize(50,50);
        divide.addActionListener(new Click());
        divide.setActionCommand("÷");
        
        clr = new JButton("CLR");
        clr.setLocation(205, 120);
        clr.setSize(75,50);
        clr.addActionListener(new Click());
        clr.setActionCommand("clr");
        
        equals = new JButton("=");
        equals.setLocation(205,175);
        equals.setSize(75,50);
        equals.addActionListener(new Click());
        equals.setActionCommand("=");
        
     
        panel.add(screen);
        
        panel.add(button1);
        panel.add(button2);
        panel.add(button3);
        panel.add(button4);
        panel.add(button5);
        panel.add(button6);
        panel.add(button7);
        panel.add(button8);
        panel.add(button9);
        
        panel.add(add);
        panel.add(subtract);
        panel.add(multiply);
        panel.add(divide);
        panel.add(clr);
        panel.add(equals);
        
        
        
        frame.setContentPane(panel);
        frame.setVisible(true);
        
    }
     class Click implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        String action = e.getActionCommand();
        
        String number1 = "";
        String number2 = "";
        

        if (action.equals("Action1")) {
            currentText += "1";
            
        } else if (action.equals("Action2")) {
            currentText += "2";
            number1 = screen.getText();
        } else if (action.equals("Action3")) {
            currentText += "3";
        } else if (action.equals("Action4")) {
            currentText += "4";
        } else if (action.equals("Action5")) {
            currentText += "5";
        } else if (action.equals("Action6")) {
            currentText += "6";
        } else if (action.equals("Action7")) {
            currentText += "7";
        } else if (action.equals("Action8")) {
            currentText += "8";
        } else if (action.equals("Action9")) {
            currentText += "9";
        } else if(action.equals("+")){
            currentText += "+";
        } else if (action.equals("-")){
            currentText += "-";
        } else if (action.equals("*")){
            currentText += "*";
        } else if (action.equals("÷")){
            currentText += "÷";
        } else if (action.equals("clr")){
            currentText = "";
        } else if (action.equals("=")){
           
        }
        
        

        screen.setText(currentText);
    }
}

    
    public static void main(String[] args) {
        
        Calculator gui = new Calculator();
        
    }
    
}




  • 1
    IMHO, this is not an exercise for a beginner... You would be better off learning about loops (to avoid code repetitions). – DYZ May 04 '23 at 16:38
  • Does this answer your question? [Parsing an arithmetic expression and building a tree from it in Java](https://stackoverflow.com/questions/4589951/parsing-an-arithmetic-expression-and-building-a-tree-from-it-in-java) – Filburt May 04 '23 at 16:39
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community May 04 '23 at 20:41

2 Answers2

0

I see that you have added value to currentText whenever a button is clicked. So my suggestion is you should do the logic based on the value of currentText.

For example:

var currentText = "1304+555";
var numbers = currentText.split("\\+|\\-|\\*|\\/"); // Split by +, -, *, /

// Replace all numbers with empty string, so that we can get the operator
var operator = currentText.replaceAll("[0-9]", "");

switch (operator) {
  case "+":
    System.out.println(
      Integer.parseInt(numbers[0]) + Integer.parseInt(numbers[1])
    );
    break;
  case "-":
    System.out.println(
      Integer.parseInt(numbers[0]) - Integer.parseInt(numbers[1])
    );
    break;
  case "*":
    System.out.println(
      Integer.parseInt(numbers[0]) * Integer.parseInt(numbers[1])
    );
    break;
  case "/":
    System.out.println(
      Integer.parseInt(numbers[0]) / Integer.parseInt(numbers[1])
    );
    break;
  default:
    System.out.println("Invalid operator");
    break;
}
0

Thank you to everyone for your help, I was able to figure it out on my own, just by using some logic and reading. Here is my final code if anyone is wondering:

package calculator;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;


public class Calculator {

String currentText = "";
JTextField screen;

JButton button1;
JButton button2;
JButton button3;
JButton button4;
JButton button5;
JButton button6;
JButton button7;
JButton button8;
JButton button9;

JButton add;
JButton subtract;
JButton multiply;
JButton divide;
JButton clr;
JButton equals;

JPanel panel;
JFrame frame;


public Calculator(){
    
    frame = new JFrame("calculator");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocation(100, 100);
    frame.setSize(300, 425);

    
    //set up panel
    panel = new JPanel();
    panel.setLayout(null);
    
    
    
    screen = new JTextField("0");
    screen.setLocation(25,25);
    screen.setSize(175,50);
      
    button1 = new JButton("1");
    button1.setLocation(25,100);
    button1.setSize(50,50);
    button1.addActionListener(new Click());
    button1.setActionCommand("Action1");
    
    button2 = new JButton("2");
    button2.setLocation(25,155);
    button2.setSize(50,50);
    button2.addActionListener(new Click());
    button2.setActionCommand("Action2");
    
    button3 = new JButton("3");
    button3.setLocation(25,210);
    button3.setSize(50,50);
    button3.addActionListener(new Click());
    button3.setActionCommand("Action3");
    
    button4 = new JButton("4");
    button4.setLocation(25,265);
    button4.setSize(50,50);
    button4.addActionListener(new Click());
    button4.setActionCommand("Action4");
    
    button5 = new JButton("5");
    button5.setLocation(80,100);
    button5.setSize(50,50);
    button5.addActionListener(new Click());
    button5.setActionCommand("Action5");
    
    button6 = new JButton("6");
    button6.setLocation(80,155);
    button6.setSize(50,50);
    button6.addActionListener(new Click());
    button6.setActionCommand("Action6");
    
    button7 = new JButton("7");
    button7.setLocation(80,210);
    button7.setSize(50,50);
    button7.addActionListener(new Click());
    button7.setActionCommand("Action7");
    
    button8 = new JButton("8");
    button8.setLocation(80,265);
    button8.setSize(50,50);
    button8.addActionListener(new Click());
    button8.setActionCommand("Action8");
    
    button9 = new JButton("9");
    button9.setLocation(50,320);
    button9.setSize(50,50);
    button9.addActionListener(new Click());
    button9.setActionCommand("Action9");
    
    add = new JButton("+");
    add.setLocation(150,100);
    add.setSize(50,50);
    add.addActionListener(new Click());
    add.setActionCommand("+");
    
    subtract = new JButton("-");
    subtract.setLocation(150,155);
    subtract.setSize(50,50);
    subtract.addActionListener(new Click());
    subtract.setActionCommand("-");
    
    multiply = new JButton("*");
    multiply.setLocation(150, 210);
    multiply.setSize(50,50);
    multiply.addActionListener(new Click());
    multiply.setActionCommand("*");
    
    divide = new JButton("÷");
    divide.setLocation(150,265);
    divide.setSize(50,50);
    divide.addActionListener(new Click());
    divide.setActionCommand("÷");
    
    clr = new JButton("CLR");
    clr.setLocation(205, 120);
    clr.setSize(75,50);
    clr.addActionListener(new Click());
    clr.setActionCommand("clr");
    
    equals = new JButton("=");
    equals.setLocation(205,175);
    equals.setSize(75,50);
    equals.addActionListener(new Click());
    equals.setActionCommand("=");
    
 
    panel.add(screen);
    
    panel.add(button1);
    panel.add(button2);
    panel.add(button3);
    panel.add(button4);
    panel.add(button5);
    panel.add(button6);
    panel.add(button7);
    panel.add(button8);
    panel.add(button9);
    
    panel.add(add);
    panel.add(subtract);
    panel.add(multiply);
    panel.add(divide);
    panel.add(clr);
    panel.add(equals);
    
    
    
    frame.setContentPane(panel);
    frame.setVisible(true);
    
}
 class Click implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
    String action = e.getActionCommand();
    
    String number1 = "";
    String number2 = "";
    

    if (action.equals("Action1")) {
        currentText += "1";
        
    } else if (action.equals("Action2")) {
        currentText += "2";
        number1 = screen.getText();
    } else if (action.equals("Action3")) {
        currentText += "3";
    } else if (action.equals("Action4")) {
        currentText += "4";
    } else if (action.equals("Action5")) {
        currentText += "5";
    } else if (action.equals("Action6")) {
        currentText += "6";
    } else if (action.equals("Action7")) {
        currentText += "7";
    } else if (action.equals("Action8")) {
        currentText += "8";
    } else if (action.equals("Action9")) {
        currentText += "9";
    } else if(action.equals("+")){
        currentText += "+";
    } else if (action.equals("-")){
        currentText += "-";
    } else if (action.equals("*")){
        currentText += "*";
    } else if (action.equals("÷")){
        currentText += "÷";
    } else if (action.equals("clr")){
        currentText = "";
    } else if (action.equals("=")){
       
    }
   
    screen.setText(currentText);
}

}

public static void main(String[] args) {
    
    Calculator gui = new Calculator();
     
  }

 }