how would i add order of operations to my GUI calculator in java? It can already do multiple expressions with any combination of operators but wont solve them in the right order. (it solves them left to right)
update: 6/29 turns out this was answered in another question. Thanks to everyone who helped!
The code is below
import javax.swing.JFrame;
//import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Main {
private static JTextField textField;
public static void main(String[] args) {
JFrame frame1 = new JFrame();
frame1.setVisible(true);
frame1.setSize(261, 409);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setTitle("Calculator");
frame1.setResizable(false);
frame1.getContentPane().setLayout(null);
textField = new JTextField();
textField.setHorizontalAlignment(SwingConstants.CENTER);
textField.setBounds(17, 9, 214, 34);
frame1.getContentPane().add(textField);
textField.setColumns(10);
JButton btnNewButton = new JButton("1");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + 1);
}
});
btnNewButton.setBounds(32, 56, 53, 43);
frame1.getContentPane().add(btnNewButton);
JButton button = new JButton("3");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e2) {
textField.setText(textField.getText() + 3);
}
});
button.setBounds(162, 56, 53, 43);
frame1.getContentPane().add(button);
JButton button_1 = new JButton("2");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e1) {
textField.setText(textField.getText() + 2);
}
});
button_1.setBounds(97, 56, 53, 43);
frame1.getContentPane().add(button_1);
JButton button_2 = new JButton("4");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e3) {
textField.setText(textField.getText() + 4);
}
});
button_2.setBounds(32, 112, 53, 43);
frame1.getContentPane().add(button_2);
JButton button_3 = new JButton("7");
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e6) {
textField.setText(textField.getText() + 7);
}
});
button_3.setBounds(32, 168, 53, 43);
frame1.getContentPane().add(button_3);
JButton button_4 = new JButton("6");
button_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e5) {
textField.setText(textField.getText() + 6);
}
});
button_4.setBounds(162, 112, 53, 43);
frame1.getContentPane().add(button_4);
JButton button_5 = new JButton("9");
button_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e8) {
textField.setText(textField.getText() + 9);
}
});
button_5.setBounds(162, 168, 53, 43);
frame1.getContentPane().add(button_5);
JButton button_6 = new JButton("5");
button_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e4) {
textField.setText(textField.getText() + 5);
}
});
button_6.setBounds(97, 112, 53, 43);
frame1.getContentPane().add(button_6);
JButton button_7 = new JButton("8");
button_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e7) {
textField.setText(textField.getText() + 8);
}
});
button_7.setBounds(97, 168, 53, 43);
frame1.getContentPane().add(button_7);
JButton btnClear = new JButton("0");
btnClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e10) {
textField.setText(textField.getText() + 0);
}
});
btnClear.setBounds(97, 224, 53, 43);
frame1.getContentPane().add(btnClear);
JButton btnClear_1 = new JButton("C");
btnClear_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e9) {
textField.setText("");
}
});
btnClear_1.setBounds(32, 224, 53, 43);
frame1.getContentPane().add(btnClear_1);
JButton btnEnter = new JButton("=");
btnEnter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e11) {
try {
int size = 0;
String string = textField.getText();
for (int i = 0; i < string.length(); i++) {
if (string.charAt(i) == '+') {
size++;
}
if (string.charAt(i) == '-') {
size++;
}
if (string.charAt(i) == '*') {
size++;
}
if (string.charAt(i) == '/') {
size++;
}
}
/* what operators are there */char[] listChar = new char[size];
/* what is the index of those operators */int[] addchar = new int[size];
/* what are the number to be solved */int[] nums = new int[size + 1];
int count = 0;
for (int i = 0; i < string.length(); i++) {
if (string.charAt(i) == '+') {
addchar[count] = i;
listChar[count] = '+';
count++;
}
if (string.charAt(i) == '-') {
addchar[count] = i;
listChar[count] = '-';
count++;
}
if (string.charAt(i) == '*') {
addchar[count] = i;
listChar[count] = '*';
count++;
}
if (string.charAt(i) == '/') {
addchar[count] = i;
listChar[count] = '/';
count++;
}
}
for (int i = 0; i < size; i++) {
String sub1 = string.substring(0, addchar[i]);
nums[i] = Integer.parseInt(sub1);
string = string.substring(addchar[i] + 1,
string.length());
for (int j = size - 1; j >= 0; j--) {
addchar[j] -= addchar[i] + 1;
}
}
nums[nums.length - 1] = Integer.parseInt(string);
int total = 0;
for (int i = 0; i < size - 1; i++) {
if (listChar[i] == '+') {
total = nums[0] + nums[1];
}
if (listChar[i] == '-') {
total = nums[0] - nums[1];
}
if (listChar[i] == '*') {
total = nums[0] * nums[1];
}
if (listChar[i] == '/') {
total = nums[0] / nums[1];
}
for (int j = 0; j < nums.length - 1; j++) {
nums[j] = nums[j + 1];
}
nums[0] = total;
}
if (listChar[listChar.length - 1] == '+') {
nums[0] += nums[1];
}
if (listChar[listChar.length - 1] == '-') {
nums[0] -= nums[1];
}
if (listChar[listChar.length - 1] == '*') {
nums[0] *= nums[1];
}
if (listChar[listChar.length - 1] == '/') {
nums[0] /= nums[1];
}
textField.setText(((Integer) nums[0]).toString());
} catch (Exception e) {
/*
* e.printStackTrace(); JOptionPane.showMessageDialog(null,
* "please enter correct statements");
*/
}
}
});
btnEnter.setBounds(162, 224, 53, 43);
frame1.getContentPane().add(btnEnter);
JButton btnNewButton_1 = new JButton("+");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e12) {
textField.setText(textField.getText() + "+");
}
});
btnNewButton_1.setBounds(32, 280, 70, 34);
frame1.getContentPane().add(btnNewButton_1);
JButton button_8 = new JButton("-");
button_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e13) {
textField.setText(textField.getText() + "-");
}
});
button_8.setBounds(145, 280, 70, 34);
frame1.getContentPane().add(button_8);
JButton button_9 = new JButton("/");
button_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e15) {
textField.setText(textField.getText() + "/");
}
});
button_9.setBounds(145, 327, 70, 34);
frame1.getContentPane().add(button_9);
JButton button_10 = new JButton("*");
button_10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e14) {
textField.setText(textField.getText() + "*");
}
});
button_10.setBounds(32, 327, 70, 34);
frame1.getContentPane().add(button_10);
}
}