title: Java calculator with multiple operations
body: <p>I have the following so far which works fine. Im sure there is probably an easier way to do this but what I will need to change/alter is the top method of Matheq. The Math method does the single math operations.</p>
  
  <p>The it does work with any single operation with +, -, *, and /.</p>
  
  <p>My problem is solving a bigger equation such as 10 - 10 / 5 + 3.
  It does however solve 10 / 5 + 65 * 2 properly.
  The reason is that each part, numbers and operations, are split to a string array. After each operation is complete the numbers and operation is replaced with the result of that equation. May seem more confusing but I couldn't think of any better way. The reason why it can't solve the other equation is because of how I mapped the strings back into string array.</p>
  
  <p>Example of string arrays
  Ex. with 10 - 10 / 5 + 3
  String = { 10, -, 10, /, 5, +, 3 }
  after operations does division first then left to right subtraction then addition
  String = { 8, 8, 2, 2, 2, 5, 5 }</p>
  
  <p>Here is my code and someone please help me:</p>
  
  <p><em><strong>REVISED</em></strong>
  revised, now it works with above but still has some trouble with LONG equations.
  A short example is that it solves 2 * 2 * 2 * 2 divided by 5 just fine but if change it so
  10 - 2 * 2 * 2 * 2 divided by 5 I get wrong answer.</p>
  
  <pre><code> public class Matheq {
  
  String fnum = null;
  String lnum = null;
  String total = null;
  
  public String Matheq(String mathoperation) {
  
      String mathoperation= "6 * 3 - 4 * 2";
  
      mathoperation = mathoperation.replaceAll(",", "");
      mathoperation = mathoperation.replaceAll("plus", "+");
      mathoperation = mathoperation.replaceAll("minus", "-");
      mathoperation = mathoperation.replaceAll("times", "*");
      mathoperation = mathoperation.replaceAll("divided by", "dividedby");
      mathoperation = mathoperation.replaceAll("percent of", "percentof");
      String[] splitstr = mathoperation.split(" ");
      while(splitstr.length&gt;1){
      for(int i=0; i&lt;splitstr.length; i++) {
          System.out.println("Get value: " + splitstr[i]);
          if(splitstr[i].indexOf("percentof") &gt;= 0) {
          String buildit = splitstr[i-1] + " percent of " + splitstr[i+1];
          String done = math(buildit);
          System.out.println("Percentage operation: " + splitstr[i-1] + " percent of " + splitstr[i+1] + "=" + done);
          splitstr[i] = done;
          splitstr[i-1] = "";
          splitstr[i+1] = "";
          ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();
          for(String s : splitstr){
              if(!s.equals("")){
                  list.add(s);
              }
          }
          splitstr = list.toArray(new String[list.size()]);
          }
      }
      for(int i=0; i&lt;splitstr.length; i++) {
          System.out.println("Get value: " + splitstr[i]);
          if(splitstr[i].indexOf("dividedby") &gt;= 0) {
              String buildit = splitstr[i-1] + " divided by " + splitstr[i+1];
              String done = math(buildit);
              System.out.println("Division operation: " + splitstr[i-1] + " divided by " + splitstr[i+1] + "=" + done);
              splitstr[i] = done;
              splitstr[i-1] = "";
              splitstr[i+1] = "";
              ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();
              for(String s : splitstr){
                  if(!s.equals("")){
                      list.add(s);
                  }
              }
              splitstr = list.toArray(new String[list.size()]);
          }
      }
      for(int i=0; i&lt;splitstr.length; i++) {
          System.out.println("Get value: " + splitstr[i]);
          if(splitstr[i].indexOf("*") &gt;= 0) {
              String buildit = splitstr[i-1] + " * " + splitstr[i+1];
              String done = math(buildit);
              System.out.println("Multiplication operation: "+ splitstr[i-1] + " * " + splitstr[i+1] + "=" + done);
              splitstr[i] = done;
              splitstr[i-1] = "";
              splitstr[i+1] = "";
              ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();
              for(String s : splitstr){
                  if(!s.equals("")){
                      list.add(s);
                  }
              }
              splitstr = list.toArray(new String[list.size()]);
          }
      }
      for(int i=0; i&lt;splitstr.length; i++) {
              System.out.println("Get value: " + splitstr[i]);
          if(splitstr[i].indexOf("+") &gt;= 0) {
              String buildit = splitstr[i-1] + " + " + splitstr[i+1];
              String done = math(buildit);
              System.out.println("Addition operation: " + splitstr[i-1] + " + " + splitstr[i+1] + "=" + done);
              splitstr[i] = done;
              splitstr[i-1] = "";
              splitstr[i+1] = "";
              ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();
              for(String s : splitstr){
                  if(!s.equals("")){
                      list.add(s);
                  }
              }
              splitstr = list.toArray(new String[list.size()]);
          }
      }
          for(int i=0; i&lt;splitstr.length; i++) {
              System.out.println("Get value: " + splitstr[i]);
              if(splitstr[i].indexOf("-") &gt;= 0) {
              String buildit = splitstr[i-1] + " - " + splitstr[i+1];
              String done = math(buildit);
              System.out.println("Subtraction operation: " + splitstr[i-1] + " - " + splitstr[i+1] + "=" + done);
              splitstr[i] = done;
              splitstr[i-1] = "";
              splitstr[i+1] = "";
              ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();
              for(String s : splitstr){
                  if(!s.equals("")){
                      list.add(s);
                  }
              }
              splitstr = list.toArray(new String[list.size()]);
          }   
      }
      for(int i=0; i&lt;splitstr.length; i++) {  
          System.out.println("Final operation: " + total + " " + splitstr[i]);
      }
      }
      return total;
  
  }
  
  private String math(String mathoperation) {
      // TODO Auto-generated method stub
      if(mathoperation.contains("percent of")){
          mathoperation = mathoperation.replaceAll("percent of", "%");
          int str = mathoperation.indexOf("%");
          System.out.println(str);
          fnum = mathoperation.substring(0, str-1);
          fnum = fnum.replaceAll(" ", "");
          fnum = "." + fnum;
          System.out.println(fnum);
          double intfnum = Double.parseDouble(fnum);
          System.out.println(intfnum);
          int lastind = mathoperation.length();
          System.out.println(lastind);
          lnum = mathoperation.substring(str+1, lastind);
          lnum = lnum.replaceAll(" ", "");
          System.out.println(lnum);
          double intlnum = Double.parseDouble(lnum);
          System.out.println(intlnum);
          double tot = intlnum * intfnum;
          System.out.println(tot);
          total = Double.toString(tot);
          if(total.length() == 3){
              total = total + "0";
          }
          if(total.length() &gt; 5){
              total = total.substring(0, 4);
          }
          total = total.replace("0.", "");
          System.out.println("Total:" + total);
          } else 
      if(mathoperation.contains("-")){
          int str = mathoperation.indexOf("-");
          System.out.println(str);
          fnum = mathoperation.substring(0, str-1);
          fnum = fnum.replaceAll(" ", "");
          System.out.println(fnum);
          double intfnum = Double.parseDouble(fnum);
          System.out.println(intfnum);
          int lastind = mathoperation.length();
          System.out.println(lastind);
          lnum = mathoperation.substring(str+1, lastind);
          lnum = lnum.replaceAll(" ", "");
          System.out.println(lnum);
          double intlnum = Double.parseDouble(lnum);
          System.out.println(intlnum);
          double tot = intfnum - intlnum; 
          System.out.println(tot);
          total = Double.toString(tot);
          System.out.println(total);
          } else 
      if(mathoperation.contains("+")){
          int str = mathoperation.indexOf("+");
          System.out.println(str);
          fnum = mathoperation.substring(0, str-1);
          fnum = fnum.replaceAll(" ", "");
          System.out.println(fnum);
          double intfnum = Double.parseDouble(fnum);
          System.out.println(intfnum);
          int lastind = mathoperation.length();
          System.out.println(lastind);
          lnum = mathoperation.substring(str+1, lastind);
          lnum = lnum.replaceAll(" ", "");
          System.out.println(lnum);
          double intlnum = Double.parseDouble(lnum);
          System.out.println(intlnum);
          double tot = intfnum + intlnum; 
          System.out.println(tot);
          total = Double.toString(tot);
          System.out.println(total);
          } else 
      if(mathoperation.contains("*")){
      int str = mathoperation.indexOf("*");
      System.out.println(str);
      fnum = mathoperation.substring(0, str-1);
      fnum = fnum.replaceAll(" ", "");
      System.out.println(fnum);
      double intfnum = Double.parseDouble(fnum);
      System.out.println(intfnum);
      int lastind = mathoperation.length();
      System.out.println(lastind);
      lnum = mathoperation.substring(str+1, lastind);
      lnum = lnum.replaceAll(" ", "");
      System.out.println(lnum);
      double intlnum = Double.parseDouble(lnum);
      System.out.println(intlnum);
      double tot = intfnum * intlnum; 
      System.out.println(tot);
      total = Double.toString(tot);
      System.out.println(total);
      } else
      if(mathoperation.contains("divided by")){
      mathoperation = mathoperation.replaceAll("divided by", "/");
      int str = mathoperation.indexOf("/");
      System.out.println(str);
      fnum = mathoperation.substring(0, str-1);
      fnum = fnum.replaceAll(" ", "");
      System.out.println(fnum);
      double intfnum = Double.parseDouble(fnum);
      System.out.println(intfnum);
      int lastind = mathoperation.length();
      System.out.println(lastind);
      lnum = mathoperation.substring(str+1, lastind);
      lnum = lnum.replaceAll(" ", "");
      System.out.println(lnum);
      double intlnum = Double.parseDouble(lnum);
      System.out.println(intlnum);
      double tot = intfnum / intlnum; 
      System.out.println(tot);
      total = Double.toString(tot);
      System.out.println(total);
      } else {
          total = null;
      }
      return total;
  }
  
  }
  </code></pre>