I am currently trying to make a calculator in Java. I want to use the String split method to tokenize the string of characters inputted. I thought I was using the String split method wrongly, because I had surrounded the characters I wanted to delimit with square brackets. However, when I removed the square brackets, the code threw an exception when I pressed the equal button. The exception was a PatternSyntaxException exception. Am I using the String split method wrongly? And why is the exception thrown? Here is my code:
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.equals("(")) { out = out + "("; textField.setText(out); } else if (output.equals(")")) { out = out + ")"; textField.setText(out); } else if (output.equals("C")) { textField.setText(" "); out = ""; } else if (output.equals("arcsin")) { out = out + "asin("; textField.setText(out); } else if (output.equals("arccos")) { out = out + "acos("; textField.setText(out); } else if (output.equals("arctan")) { out = out + "atan("; textField.setText(out); } else if (output.equals("log")) { out = out + "log("; textField.setText(out); } else if (output.equals("seven")) { out = out + "7"; textField.setText(out); } else if (output.equals("eight")) { out = out + "8"; textField.setText(out); } else if (output.equals("nine")) { out = out + "9"; textField.setText(out); } else if (output.equals("four")) { out = out + "4"; textField.setText(out); } else if (output == "five") { out = out + "5"; textField.setText(out); } else if (output.equals("six")) { out = out + "6"; textField.setText(out); } else if (output.equals("one")) { out = out + "1"; textField.setText(out); } else if (output.equals("two")) { out = out + "2"; textField.setText(out); } else if (output.equals("three")) { out = out + "3"; textField.setText(out); } else if (output.equals("zero")) { out = out + "0"; textField.setText(out); } else if (output.equals("radixpoint")) { out = out + "."; textField.setText(out); } else if (output.equals("equal")) { String[] numbers = out.split("+-x/doubleintarclg", 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.equals("x")) { answer = answer + realnumbers[Arrays.asList(operations).indexOf(op)]*realnumbers[Arrays.asList(operations).indexOf(op)+1]; } if (op.equals("/")) { answer = answer + realnumbers[Arrays.asList(operations).indexOf(op)]/realnumbers[Arrays.asList(operations).indexOf(op)+1]; } if (op.equals("+")) { answer = answer + realnumbers[Arrays.asList(operations).indexOf(op)]+realnumbers[Arrays.asList(operations).indexOf(op)+1]; } if (op.equals("-")) { 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.equals("plus")) { out = out + "+"; textField.setText(out); } else if (output.equals("subtract")) { out = out + "-"; textField.setText(out); } else if (output.equals("multiply")) { out = out + "x"; textField.setText(out); } else if (output.equals("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); } }