title: Brackets in a calculator for java
body: <p>I am trying to implement a calculator that works just like the normal calculators, I managed to block out many inputs from keyboard to prevent unnecessary characters, Then i managed to analyze the textfield content and put the content in an arraylist. The array list contains operators including brackets and put in the array list as well. Now I want to be able to take the brackets and perform multiplication for them or perform the operator that comes before it. My code for the bracket analyzer is as below</p>
  
  <pre><code>private void BracketSolver(int opnBracketPosition, ArrayList Analyzed)
  {Boolean _closed=true;
      for (int i=opnBracketPosition;i&lt;Analyzed.size();i++)//check all array element
      {
          if (Analyzed.get(i).equals("("))//if its an open bracket
          {
               _closed = false;//a bracket opened
               if(Analyzed.get(i+1).equals("-")){//if a minus after the bracket, then turn the number after the minus to a negative and delete the minus
               Analyzed.set(i+2,String.valueOf((-1)*Double.parseDouble(Analyzed.get(i+2).toString())));
               Analyzed.remove(i+1);}
              for (int j=i+1;j&lt;Analyzed.size();j++){//check all element after the open bracket
                  if(Analyzed.get(j).equals("("))
                  {
  
                      BracketSolver(j, Analyzed);i=0;
                  }//if we meet another open bracket, then restart the function from there
                  else if(Analyzed.get(j).equals(")"))//but if its a closing
                  {
                      _closed = true;//it has been closed
                      if(i!=0){//make sure is not zero to prevent error of i-1=-1
                     if((Analyzed.get(i-1).equals("+"))||(Analyzed.get(i-1).equals("-"))
                              ||(Analyzed.get(i-1).equals("x"))||(Analyzed.get(i-1).equals("^"))
                              ||(Analyzed.get(i-1).equals("÷"))||(Analyzed.get(i-1).equals("√"))) {
                         //if an operator is before the bracket then just delete the brackets and calculate
                      Analyzed.remove(j);Analyzed.remove(i);//remove the brackets
                      Calculator_Basic(Analyzed, i, j-1);//solve for all after the open bracket n before the close     
                  }
                      else{Analyzed.remove(j);Analyzed.set(i,String.valueOf('x'));
                      //remove the close bracket but change the open bracket to multiply
                      Calculator_Basic(Analyzed, i+1, j-1);//solve for all after the open bracket n before the close
                  }
                  }else{    Analyzed.remove(j);Analyzed.remove(i);//remove the brackets
                      Calculator_Basic(Analyzed, i, j-1);//solve for all after the open bracket n before the close
                  }//i=0;//restart
                  }
             }
          }
      }  if(!_closed)//if it has ended and still no close
                   {throw new IllegalArgumentException("Syntax Error: Bracket Not Closed");
                   }
  }
  </code></pre>
  
  <p>the function calculator_Basic is another function that takes an arraylist and a start and end integer and calculates everything in between. It already works fine but if you need it to help me out i can paste it as well, though it is very plenty and rough. Now my problem is this code cannot perform on something like (5)(8). I do not know why. I know my code is plenty and might be uneasy to understand.
  The next set of code is what I used in storing the text field into an array list</p>
  
  <pre><code>private static ArrayList&lt;String&gt; AnalyzedContent;
  private void Analyzer()//function that analyzes the user input char by char and takes the numbers
  {
      AnalyzedContent = new ArrayList();//to store everything
     char[] operators = {'+','-','x','÷','^','√','(',')'};//array holding all operators
     String set = "";//string to hold the numbers
      for (int i=0;i&lt;Display.getText().length();i++)//for all chars in display
      {//if its an operator
  if((Display.getText().charAt(i)==operators[0])||(Display.getText().charAt(i)==operators[1])||(Display.getText().charAt(i)==operators[2])
          ||(Display.getText().charAt(i)==operators[3])||(Display.getText().charAt(i)==operators[4])||(Display.getText().charAt(i)==operators[5])
          ||(Display.getText().charAt(i)==operators[6])||(Display.getText().charAt(i)==operators[7]))
                  {
                  if (!set.equals(""))AnalyzedContent.add(set);//check if set is not empty then store its content
                      set = "";//reset the set
                      for (int j = 0; j&lt;operators.length;j++){//check which operator it is and store
                          if (Display.getText().charAt(i)==operators[j])
                          {
                              if((i==0)&amp;&amp;(j!=6))throw new ArithmeticException("Syntax Error::Cannot Begin with Operator");
                          //if it is start of the display and operator is not open bracket then throw error
                              AnalyzedContent.add(operators[j] + "");
                          }}
                       //j=operators.length;//leave the loop when something is found
                  }
                  else//else if its a number append to set which might already contain numbers not separated by an operator
                  {
                     set = set + Display.getText().charAt(i);//append the number to previous
                  }
          }if (!set.equals(""))AnalyzedContent.add(set);//check if set is not empty then store its content
  </code></pre>
  
  <p>thanks in advance anyone
      Again, My if statement for the operators using || is very rough, I am guessing there is a neater way to do it, if someone could help me there too.</p>