title: Java GUI Scientific Calculator
body: <p>I am trying to code a Scientific calculator in Java. However, I am having difficulties trying to actually get the program to calculate. You can see in the code below, I have a managed to get the GUI working with the numbers printing on the GUI. I have also added functionality to the "+" button in order to test it first to see if the program works.</p>
  
  <pre><code>public class Gui extends JFrame {
      private static JButton [] button = new JButton[36];
      private static TextField tf;
      private JPanel panel;
      private JPanel panel1;
  
  
  
  public Gui(){
      super("Scientific Calculator");
      panel = new JPanel();
      panel1 = new JPanel(new GridLayout(0,4));
  
      tf = new TextField(20);
      tf.setEditable(false);
      panel.add(tf);
  
      button[0]=new JButton("Rad/Deg");
      button[1]=new JButton("x!");
      button[2]=new JButton("sqrt");
      button[3]=new JButton("sin");
      button[4]=new JButton("cos");
      button[5]=new JButton("tan");
      button[6]=new JButton("ln");
      button[7]=new JButton("log");
      button[8]=new JButton("1/x");
      button[9]=new JButton("e^x");
      button[10]=new JButton("x^2");
      button[11]=new JButton("y^x");
      button[12]=new JButton("|x|");
      button[13]=new JButton("pi");
      button[14]=new JButton("e");
  
      button[15]=new JButton("C");
      button[16]=new JButton("(");
      button[17]=new JButton(")");
      button[18]=new JButton("%");
      button[19]=new JButton("/");
      button[20]=new JButton("7");
      button[21]=new JButton("8");
      button[22]=new JButton("9");
      button[23]=new JButton("*");
      button[24]=new JButton("4");
      button[25]=new JButton("5");
      button[26]=new JButton("6");
      button[27]=new JButton("-");
      button[28]=new JButton("1");
      button[29]=new JButton("2");
      button[30]=new JButton("3");
      button[31]=new JButton("+");
      button[32]=new JButton(".");
      button[33]=new JButton("0");
      button[34] =new JButton("+/-");
      button[35] =new JButton("=");
  
  
  
      for(int i = 0; i&lt;button.length;i++){
          panel1.add(button[i]);
      }
  
  
      panel.add(panel1);
      add(panel);
  
      HandlerClass handler = new HandlerClass();
      for(int i = 0; i&lt;button.length;i++){
          button[i].addActionListener(handler);
      }
  
  }
  private class HandlerClass implements ActionListener{
  
      @Override
      public void actionPerformed(ActionEvent e) {
          double num1 = 0,num2;
  
          String alreadyDisplayed = tf.getText();
          String toDisplay = "";
  
          if(e.getSource()==button[33]){
              alreadyDisplayed = tf.getText();
              toDisplay = alreadyDisplayed + (button[33].getText());
              tf.setText(toDisplay);
          }
          if(e.getSource()==button[28]){
               alreadyDisplayed = tf.getText();
               toDisplay = alreadyDisplayed + (button[28].getText());
              tf.setText(toDisplay);
          }
          if(e.getSource()==button[29]){
              alreadyDisplayed = tf.getText();
              toDisplay = alreadyDisplayed + (button[29].getText());
              tf.setText(toDisplay);
          }
          if(e.getSource()==button[30]){
              alreadyDisplayed = tf.getText();
              toDisplay = alreadyDisplayed + (button[30].getText());
              tf.setText(toDisplay);
          }
          if(e.getSource()==button[24]){
              alreadyDisplayed = tf.getText();
              toDisplay = alreadyDisplayed + (button[24].getText());
              tf.setText(toDisplay);
          }
          if(e.getSource()==button[25]){
              alreadyDisplayed = tf.getText();
              toDisplay = (button[25].getText());
              tf.setText(toDisplay);
          }
          if(e.getSource()==button[26]){
              alreadyDisplayed = tf.getText();
              toDisplay = alreadyDisplayed + (button[26].getText());
              tf.setText(toDisplay);
          }
          if(e.getSource()==button[20]){
              alreadyDisplayed = tf.getText();
              toDisplay = alreadyDisplayed + (button[20].getText());
              tf.setText(toDisplay);
          }
          if(e.getSource()==button[21]){
              alreadyDisplayed = tf.getText();
              toDisplay = alreadyDisplayed + (button[21].getText());
              tf.setText(toDisplay);
          }
          if(e.getSource()==button[22]){
              alreadyDisplayed = tf.getText();
              toDisplay = alreadyDisplayed + (button[22].getText());
              tf.setText(toDisplay);
          }
          if(e.getSource()==button[31]){
              alreadyDisplayed = tf.getText();
              toDisplay = alreadyDisplayed + (button[31].getText());
              tf.setText(toDisplay);
          }
  </code></pre>
  
  <p>However my problem is what to do afterwards. What I am trying to do is have a try and catch block to see if the first input is a number and then convert it into a double, then store it in an array so that I can use the operator function afterwards and then keep adding numbers until the user wants to stop and perform the calculation. My attempt at this code is below, but it is wrong. Can someone please suggest what I am doing wrong? Thanks</p>
  
  <pre><code>try{
      num1 = Double.parseDouble(toDisplay);
  }
  catch(NumberFormatException e1){
      System.out.println("You have not entered a number");
  }
  double[] arr = new double[10];
  for(int i=0; i&lt;arr.length;i++){
      arr[i]=num1;
      String test = String.valueOf(arr[i]);
      tf.setText(test);
  }
  </code></pre>
  
  <p><em>EDIT</em> I have wrote new code for a simple calculator as recommended by another user and it works for two inputs. However I am still getting errors when I try to implement multiple inputs. Below is the working code for the two input calculator.</p>
  
  <pre><code>import javax.swing.*;
  
  import java.awt.*;
  import java.awt.event.*;
  
  public class CalcGui extends JFrame {
  
      private JButton buttonZero;
      private JButton buttonOne;
      private JButton buttonTwo;
      private JButton buttonThree;
      private JButton buttonFour;
      private JButton buttonFive;
      private JButton buttonSix;
      private JButton buttonSeven;
      private JButton buttonEight;
      private JButton buttonNine;
  
      private JButton opButtonPlus;
      private JButton opButtonMinus;
      private JButton opButtonDivide;
      private JButton opButtonMultiply;
      private JButton opButtonEquals;
      private JButton opButtonClear;
  
      private TextField tf;
  
      private JPanel numButtonPanel;
      private JPanel opButtonPanel;
      private JPanel basePanel;
  
      public CalcGui(){
          super("Scientific Calculator");
          basePanel = new JPanel();
          numButtonPanel = new JPanel(new GridLayout(0,4));
          opButtonPanel = new JPanel(new GridLayout(0,1));
  
          tf = new TextField(20);
          tf.setEditable(false);
          basePanel.add(tf);
  
           buttonZero = new JButton("0");
           numButtonPanel.add(buttonZero);
           buttonOne = new JButton("1");
           numButtonPanel.add(buttonOne);
           buttonTwo = new JButton("2");
           numButtonPanel.add(buttonTwo);
           buttonThree = new JButton("3");
           numButtonPanel.add(buttonThree);
           buttonFour = new JButton("4");
           numButtonPanel.add(buttonFour);
           buttonFive = new JButton("5");
           numButtonPanel.add(buttonFive);
           buttonSix = new JButton("6");
           numButtonPanel.add(buttonSix);
           buttonSeven = new JButton("7");
           numButtonPanel.add(buttonSeven);
           buttonEight = new JButton("8");
           numButtonPanel.add(buttonEight);
           buttonNine = new JButton("9");
           numButtonPanel.add(buttonNine);
  
           basePanel.add(numButtonPanel);
  
          opButtonPlus = new JButton("+");
          opButtonMinus = new JButton("-");
          opButtonDivide = new JButton("/");
          opButtonMultiply = new JButton("*");
          opButtonEquals = new JButton("=");
          opButtonClear = new JButton("C");
  
          opButtonPanel.add(opButtonPlus);
          opButtonPanel.add(opButtonMinus);
          opButtonPanel.add(opButtonDivide);
          opButtonPanel.add(opButtonMultiply);
          opButtonPanel.add(opButtonEquals);
          opButtonPanel.add(opButtonClear);
  
          basePanel.add(opButtonPanel);
  
          add(basePanel);
  
          HandlerClass handler = new HandlerClass();
  
          buttonZero.addActionListener(handler);
          buttonOne.addActionListener(handler);
          buttonTwo.addActionListener(handler);
          buttonThree.addActionListener(handler);
          buttonFour.addActionListener(handler);
          buttonFive.addActionListener(handler);
          buttonSix.addActionListener(handler);
          buttonSeven.addActionListener(handler);
          buttonEight.addActionListener(handler);
          buttonNine.addActionListener(handler);
  
          opButtonPlus.addActionListener(handler);
          opButtonMinus.addActionListener(handler);
          opButtonDivide.addActionListener(handler);
          opButtonMultiply.addActionListener(handler);
          opButtonEquals.addActionListener(handler);
          opButtonClear.addActionListener(handler);
  
      }
  
  
      private class HandlerClass implements ActionListener{
          public void actionPerformed(ActionEvent e){
              String alreadyDisplayed = tf.getText();
              if(e.getSource()==buttonZero){
                  String buttonZeroText = alreadyDisplayed + buttonZero.getText();
                  tf.setText(buttonZeroText);
              }else if(e.getSource()==buttonOne){
                  String buttonOneText = alreadyDisplayed + buttonOne.getText();
                  tf.setText(buttonOneText);
              }else if(e.getSource()==buttonTwo){
                  String buttonTwoText = alreadyDisplayed + buttonTwo.getText();
                  tf.setText(buttonTwoText);
              }else if(e.getSource()==buttonThree){
                  String buttonThreeText = alreadyDisplayed + buttonThree.getText();
                  tf.setText(buttonThreeText);
              }else if(e.getSource()==buttonFour){
                  String buttonFourText = alreadyDisplayed + buttonFour.getText();
                  tf.setText(buttonFourText);
              }else if(e.getSource()==buttonFive){
                  String buttonFiveText = alreadyDisplayed + buttonFive.getText();
                  tf.setText(buttonFiveText);
              }else if(e.getSource()==buttonSix){
                  String buttonSixText = alreadyDisplayed + buttonSix.getText();
                  tf.setText(buttonSixText);
              }else if(e.getSource()==buttonSeven){
                  String buttonSevenText = alreadyDisplayed + buttonSeven.getText();
                  tf.setText(buttonSevenText);
              }else if(e.getSource()==buttonEight){
                  String buttonEightText = alreadyDisplayed + buttonEight.getText();
                  tf.setText(buttonEightText);
              }else if(e.getSource()==buttonNine){
                  String buttonNineText = alreadyDisplayed + buttonNine.getText();
                  tf.setText(buttonNineText);
              }else if(e.getSource()==opButtonPlus){
                  String opButtonPlusText = alreadyDisplayed + opButtonPlus.getText();
                  tf.setText(opButtonPlusText);
              }else if(e.getSource()==opButtonEquals){
                  String opButtonEqualsText = alreadyDisplayed + opButtonEquals.getText();
                  tf.setText(opButtonEqualsText);
              }else if(e.getSource()==opButtonMinus){
                  String opButtonMinusText = alreadyDisplayed + opButtonMinus.getText();
                  tf.setText(opButtonMinusText);
              }else if(e.getSource()==opButtonMultiply){
                  String opButtonMultiplyText = alreadyDisplayed + opButtonMultiply.getText();
                  tf.setText(opButtonMultiplyText);
              }else if(e.getSource()==opButtonDivide){
                  String opButtonDivideText = alreadyDisplayed + opButtonDivide.getText();
                  tf.setText(opButtonDivideText);
              }else if(e.getSource()==opButtonClear){
                  String opButtonClearText = "";
                  tf.setText(opButtonClearText);
              }
  
  
              double result;
              for(int i=0; i&lt;alreadyDisplayed.length();i++){
  
                  if(e.getSource()==opButtonEquals){
  
                      if(alreadyDisplayed.charAt(i)=='+'){
                          result = Double.parseDouble(alreadyDisplayed.substring(0,i)) + Double.parseDouble(alreadyDisplayed.substring(i+1,alreadyDisplayed.length()));
                          tf.setText(alreadyDisplayed + "=" + result);
                      }else if(alreadyDisplayed.charAt(i)=='-'){
                          result = Double.parseDouble(alreadyDisplayed.substring(0,i)) - Double.parseDouble(alreadyDisplayed.substring(i+1,alreadyDisplayed.length()));
                          tf.setText(alreadyDisplayed + "=" + result);
                      }else if(alreadyDisplayed.charAt(i)=='/'){
                          result = Double.parseDouble(alreadyDisplayed.substring(0,i)) / Double.parseDouble(alreadyDisplayed.substring(i+1,alreadyDisplayed.length()));
                          tf.setText(alreadyDisplayed + "=" + result);
                      }else if(alreadyDisplayed.charAt(i)=='*'){
                          result = Double.parseDouble(alreadyDisplayed.substring(0,i)) * Double.parseDouble(alreadyDisplayed.substring(i+1,alreadyDisplayed.length()));
                          tf.setText(alreadyDisplayed + "=" + result);
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>The part I am having difficulties with is getting a number and storing it so that I can let a user enter more than two values. So for example if I take out the for loop at the end of the above code and replaced it with this;</p>
  
  <pre><code>double []num1=new double[10];
      double []num2=new double[10];
      //double num1 = 0;
      double temp =0;
      String str[] = new String [10];
      String []str1 = new String[10];
      if(e.getActionCommand().equals("+"))
      if(e.getSource() == opButtonEquals)
          for( int i = 0; i&lt;alreadyDisplayed.length(); i++){
  
              if(alreadyDisplayed.charAt(i)=='+')
               str[i] = alreadyDisplayed.substring(0,i);
               num1[i] = Double.parseDouble(str[i]);
               str1[i+1] = alreadyDisplayed.substring(i+1,alreadyDisplayed.length());
               num2[i+1] = Double.parseDouble(str1[i]);
  
               temp += num1[i] + num2[i+1];
  
              tf.setText(Double.toString(temp));
  
          }System.out.println(temp);
      }
  }
  </code></pre>
  
  <p>So when I try to test to see if temp has any value in the console, it simply prints 0 out each time. The string text is showing up in the text field, however there is no actual calculation working. I thought this would work by storing a number and then storing another number and then adding them together but it has not.</p>