I am making a calculator in Java. However, when I press the "=" button on my calculator, it always returns 0.0, no matter what. I don't understand why? The code is below:
import javax.swing.*;//import the packages needed for gui import java.awt.*; import java.awt.event.*; import java.util.Arrays; import java.util.List; import static java.lang.Math.*; public class CalculatorCopy { public static void main(String[] args) { JFrame window = new JFrame("Window");//makes a JFrame window.setSize(300,415); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel (new FlowLayout());//makes the panel, textfield and buttons and gives the buttons actioncommands final JTextField textField = new JTextField(20); JButton openbracket = new JButton("("); openbracket.setActionCommand("("); JButton closebracket = new JButton(")"); closebracket.setActionCommand(")"); JButton clearbutton = new JButton("C"); clearbutton.setActionCommand("C"); JButton arcsin = new JButton("arcsin"); arcsin.setActionCommand("arcsin"); JButton arccos = new JButton("arccos"); arccos.setActionCommand("arccos"); JButton arctan = new JButton("arctan"); arctan.setActionCommand("arctan"); JButton sin = new JButton("sin"); sin.setActionCommand("sin"); JButton cos = new JButton("cos"); cos.setActionCommand("cos"); JButton tan = new JButton("tan"); tan.setActionCommand("tan"); JButton log = new JButton("log"); log.setActionCommand("log"); JButton seven = new JButton("7"); seven.setActionCommand("seven"); JButton eight = new JButton("8"); eight.setActionCommand("eight"); JButton nine = new JButton("9"); nine.setActionCommand("nine"); JButton four = new JButton("4"); four.setActionCommand("four"); JButton five = new JButton("5"); five.setActionCommand("five"); JButton six = new JButton("6"); six.setActionCommand("six"); JButton one = new JButton("1"); one.setActionCommand("one"); JButton two = new JButton("2"); two.setActionCommand("two"); JButton three = new JButton("3"); three.setActionCommand("three"); JButton zero = new JButton("0"); zero.setActionCommand("zero"); JButton radixpoint = new JButton("."); radixpoint.setActionCommand("radixpoint"); JButton plus = new JButton("+"); plus.setActionCommand("plus"); JButton subtract = new JButton("-"); subtract.setActionCommand("subtract"); JButton multiply = new JButton("x"); multiply.setActionCommand("multiply"); JButton divide = new JButton("/"); divide.setActionCommand("divide"); JButton equal = new JButton("="); equal.setActionCommand("equal"); final String values = " "; class Listener implements ActionListener { String out = ""; public void actionPerformed(ActionEvent e) { String output = e.getActionCommand(); if(output == "(") { out = out + "("; textField.setText(out); } else if (output == ")") { out = out + ")"; textField.setText(out); } else if (output == "C") { textField.setText(" "); out = ""; } else if (output == "arcsin") { out = out + "asin("; textField.setText(out); } else if (output == "arccos") { out = out + "acos("; textField.setText(out); } else if (output == "arctan") { out = out + "atan("; textField.setText(out); } else if (output == "log") { out = out + "log("; textField.setText(out); } else if (output == "seven") { out = out + "7"; textField.setText(out); } else if (output == "eight") { out = out + "8"; textField.setText(out); } else if (output == "nine") { out = out + "9"; textField.setText(out); } else if (output == "four") { out = out + "4"; textField.setText(out); } else if (output == "five") { out = out + "5"; textField.setText(out); } else if (output == "six") { out = out + "6"; textField.setText(out); } else if (output == "one") { out = out + "1"; textField.setText(out); } else if (output == "two") { out = out + "2"; textField.setText(out); } else if (output == "three") { out = out + "3"; textField.setText(out); } else if (output == "zero") { out = out + "0"; textField.setText(out); } else if (output == "radixpoint") { out = out + "."; textField.setText(out); } else if (output == "equal") { String[] numbers = out.split("[ +-x/doublesintarclg]", 0); //separate the numbers from the operations String[] operations = out.split("[ 1234567890.()]", 0); double[] realnumbers = new double[100]; //make an array of doubles double answer = 0; for ( String number: numbers ) { for (double realnumber : realnumbers ) { realnumber = Double.parseDouble(number); //make the string number a double value } } for (String op : operations ) { if (op == "x") { answer = answer + realnumbers[Arrays.asList(operations).indexOf(op)]*realnumbers[Arrays.asList(operations).indexOf(op)+1]; } if (op == "/") { answer = answer + realnumbers[Arrays.asList(operations).indexOf(op)]/realnumbers[Arrays.asList(operations).indexOf(op)+1]; } if (op == "+") { answer = answer + realnumbers[Arrays.asList(operations).indexOf(op)]+realnumbers[Arrays.asList(operations).indexOf(op)+1]; } if (op == "-") { answer = answer + realnumbers[Arrays.asList(operations).indexOf(op)]-realnumbers[Arrays.asList(operations).indexOf(op)+1]; } String stringofanswer = String.valueOf(answer); textField.setText(stringofanswer); } } else if (output == "plus") { out = out + "+"; textField.setText(out); } else if (output == "subtract") { out = out + "-"; textField.setText(out); } else if (output == "multiply") { out = out + "x"; textField.setText(out); } else if (output == "divide") { out = out + "/"; textField.setText(out); } } } Listener listener = new Listener(); //makes an object of the actionlistener panel.add(textField);//adding all the things window.add(panel); openbracket.addActionListener(listener); panel.add(openbracket); closebracket.addActionListener(listener); panel.add(closebracket); clearbutton.addActionListener(listener); panel.add(clearbutton); arcsin.addActionListener(listener); panel.add(arcsin); arccos.addActionListener(listener); panel.add(arccos); arctan.addActionListener(listener); panel.add(arctan); sin.addActionListener(listener); panel.add(sin); cos.addActionListener(listener); panel.add(cos); tan.addActionListener(listener); panel.add(tan); log.addActionListener(listener); panel.add(log); nine.addActionListener(listener); panel.add(nine); eight.addActionListener(listener); panel.add(eight); seven.addActionListener(listener); panel.add(seven); six.addActionListener(listener); panel.add(six); five.addActionListener(listener); panel.add(five); four.addActionListener(listener); panel.add(four); three.addActionListener(listener); panel.add(three); two.addActionListener(listener); panel.add(two); one.addActionListener(listener); panel.add(one); zero.addActionListener(listener); panel.add(zero); radixpoint.addActionListener(listener); panel.add(radixpoint); plus.addActionListener(listener); panel.add(plus); subtract.addActionListener(listener); panel.add(subtract); multiply.addActionListener(listener); panel.add(multiply); divide.addActionListener(listener); panel.add(divide); equal.addActionListener(listener); panel.add(equal); window.setVisible(true); } }
What's wrong with my code?