title: Simple Java Calculator to Solve Expressions
body: <p>I'm working on an calculator that solves expressions. I'm trying to figure out how to how make it calculate in PEMDAS order. I have a for loop to traverse the array list and a switch that calls a class that does the math. I've tried if statements, but haven't been able to figure it out. </p>
  
  <p>How can I change this to make sure the expression is solved in the proper order? Here's what I have so far:</p>
  
  <pre><code>/*
  Peter Harmazinski
  Simple Calculator
  
  This program solves expressions
  */
  
  import java.util.*;
  
  public class SimpleCalculator2 {
      static SimpleMath math = new SimpleMath();
  
      public static void main(String[] args) {
          Scanner console = new Scanner(System.in);
          boolean again = true;
          double number1 = 0.0;
          double number2 = 0.0;
          double answer = 0.0;
          double results = 0.0;
          String delims = "[ ]+";
  
          getIntroduction();
  
          while (again) {
              System.out.println("Please enter your expression.");
              String input = console.nextLine();
              System.out.println("This is the user's input: " + input);
  
              //Parses string into array list
              List&lt;String&gt; list = new ArrayList&lt;String&gt;(Arrays.asList(input.split(delims)));
              System.out.println("list: " + list);
  
              results = doMath(list, number1, number2);
              getResults(results);
          }
          console.close();
      }
  
      public static void getIntroduction() {
          System.out.println("This is a simple calculator that solves expressions.");
      }
  
      //Traverses array list to identify operators and does math for surrounding numbers
      //then answer is inserted in i-1 element and the elements i and i+1 are deleted
  
      public static double doMath(List&lt;String&gt; list, double number1, double number2) {
          double answer = 0.0;
          double results = 0.0;
          while (list.size() &gt; 1) {
              for (int i = 0; i &lt; list.size(); i++) {
                      switch (list.get(i)) {
                          case "*" :
                              number1 = Double.parseDouble(list.get(i - 1));
                              number2 = Double.parseDouble(list.get(i + 1));
                              answer = math.multiply(number1, number2);
                              System.out.println(answer);
                              list.add(i - 1, Double.toString(answer));
                              list.subList(i, i + 3).clear();
                              System.out.println(list);
                              break;
                          case "/" :
                              number1 = Double.parseDouble(list.get(i - 1));
                              number2 = Double.parseDouble(list.get(i + 1));
                              answer = math.divide(number1, number2);
                              System.out.println(answer);
                              list.add(i - 1, Double.toString(answer));
                              list.subList(i, i + 3).clear();
                              System.out.println(list);
                              break;
                          case "+" :
                              number1 = Double.parseDouble(list.get(i - 1));
                              number2 = Double.parseDouble(list.get(i + 1));
                              answer = math.add(number1, number2);
                              System.out.println(answer);
                              list.add(i - 1, Double.toString(answer));
                              list.subList(i, i + 3).clear();
                              System.out.println(list);
                              break;
                          case "-" :
                              number1 = Double.parseDouble(list.get(i - 1));
                              number2 = Double.parseDouble(list.get(i + 1));
                              answer = math.subtract(number1, number2);
                              System.out.println(answer);
                              list.add(i - 1, Double.toString(answer));
                              list.subList(i, i + 3).clear();
                              System.out.println(list);
                              break;
                          }
                  }   
              }   
          return answer;
      }
  
      public static void getResults(double results) {
          System.out.println("Results are: " + results);
      }
  }
  </code></pre>