title: Calculate an operation with a negative number first in a string
body: <p>I've just about written every possible combination of code I can think of, but I can't get my calculator to work if there is a negative operator first. </p>
  
  <p>The way the calculator works is if there already contains an operator (with the last character of the string being a digit) it will calculate what's already there first and then add the operator pressed. However, I want it to allow and second operator if and only if the first character is a negative (- symbol). But I can't for the life of me do this. This is what I have so far:</p>
  
  <pre><code>// calculation logic
      public void calculate() {
              for (int i = 0; i &lt; textField.length(); i++) {
                      checkOp(i);
              }
              resultDisplayed = true;
      }
  
      public float checkOp(int i) {
              if (textField.getText().charAt(i) == '+') {
                      result = Float.parseFloat(textField.getText().toString().substring(0, i)) + Float.parseFloat(textField.getText().toString().substring(i + 1, textField.length()));
                      returnAnswer(decimalFormat.format(result));
              } else if (textField.getText().charAt(i) == '-') {
                      if (textField.getText().charAt(0) != '-') {
                              result = Float.parseFloat(textField.getText().toString().substring(0, i)) - Float.parseFloat(textField.getText().toString().substring(i + 1, textField.length()));
                      } else {
  
                      }
              } else if (textField.getText().charAt(i) == '*') {
                      result = Float.parseFloat(textField.getText().toString().substring(0, i)) * Float.parseFloat(textField.getText().toString().substring(i + 1, textField.length()));
                      returnAnswer(decimalFormat.format(result));
              } else if (textField.getText().charAt(i) == '/') {
                      result = Float.parseFloat(textField.getText().toString().substring(0, i)) / Float.parseFloat(textField.getText().toString().substring(i + 1, textField.length()));
                      returnAnswer(decimalFormat.format(result));
              }
  
              return result;
      }
  
  // checks if last character is one of the operands (not -)
      public void checkLastOp(String op) {
              if (textField.length() == 0 &amp; op.equals("-")) {
                      addToOutput("-");
              } else if (textField.length() &gt; 0) {
                      if (op.equals("+")) {
                              if (textField.getText().toString().contains("+") || textField.getText().toString().contains("-") || textField.getText().toString().contains("*") || textField.getText().toString().contains("/")) {
                                      if (Character.isDigit(textField.getText().charAt(textField.length() - 1)) &amp;&amp; textField.getText().charAt(0) != '-') {
                                              calculate();
                                      } else {
                                              if (!Character.isDigit(textField.getText().charAt(textField.length() - 1))) {
                                                      textField.getText().delete(textField.getText().length() - 1, textField.getText().length());
                                              }
                                      }
                              }
                              addToOutput("+");
                      } else if (op.equals("-")) {
                              if (textField.getText().toString().contains("+") || textField.getText().toString().contains("-") || textField.getText().toString().contains("*") || textField.getText().toString().contains("/")) {
                                      if (Character.isDigit(textField.getText().charAt(textField.length() - 1))) {
                                              calculate();
                                      } else if (textField.getText().charAt(textField.length() - 1) == '+' || textField.getText().charAt(textField.length() - 1) == '-') {
                                              textField.getText().delete(textField.getText().length() - 1, textField.getText().length());
                                      }
                              }
                              addToOutput("-");
                      } else if (op.equals("*")) {
                              if (textField.getText().toString().contains("+") || textField.getText().toString().contains("-") || textField.getText().toString().contains("*") || textField.getText().toString().contains("/")) {
                                      if (Character.isDigit(textField.getText().charAt(textField.length() - 1))) {
                                              calculate();
                                      } else {
                                              textField.getText().delete(textField.getText().length() - 1, textField.getText().length());
                                      }
                              }
                              addToOutput("*");
                      } else if (op.equals("/")) {
                              if (textField.getText().toString().contains("+") || textField.getText().toString().contains("-") || textField.getText().toString().contains("*") || textField.getText().toString().contains("/")) {
                                      if (Character.isDigit(textField.getText().charAt(textField.length() - 1))) {
                                              calculate();
                                      } else {
                                              textField.getText().delete(textField.getText().length() - 1, textField.getText().length());
                                      }
                              }
                              addToOutput("/");
                      }
              }
      }
  </code></pre>
  
  <p>CheckLastOp gets called when you press either +, -, *, or / and passes the appropriate operator through the method.</p>
  
  <p>With my calculate method, I've tried checking if - is the first character, if it isn't proceed as normal, if it is calculate from the second value and minus the answer from zero.</p>
  
  <p><strong>Edit:</strong> Think I've spotted why it crashes. In my calculate method, if the first character is i, i stops at 0, it then calculates from 1 to i which is from 1 to 0, which is a complete mess. Now I need a way to go from 1 to the next character which would meaning calling calculate again, but not sure of a way to do this.</p>
  
  <p>Rewrote calculation logic to tidy it up.</p>
  
  <p><strong>Edit 2:</strong> Solved it, updated my code to show my solution.</p>