title: Substring can't find what it should find
body: <p>I have a string (replacedLigning) containing y=3-2+4x-6. I want xVærdi (an ArrayList) to contain the x value. In the example y=3-2+4x-6 I want the xVærdi to store "4", since that's how many x'es there are. The initial value of xVærdi is none, and the problem is, that substring isn't able to find the "x", so the xVærdi will not contain any elements. Or so I think. </p>
  
  <p>the i is initialized as 1</p>
  
  <pre><code>while(isNumber(replacedLigning.substring((replacedLigning.indexOf("x")-i),(replacedLigning.indexOf("x")-i+1))) == true){
      xVærdi.add(replacedLigning.substring((replacedLigning.indexOf("x")-i),(replacedLigning.indexOf("x")-i+1)));
      i++;
  }
  </code></pre>
  
  <p>And</p>
  
  <pre><code>System.out.println(xVærdi); //It doesn't contain anything. Output: []
  </code></pre>
  
  <p>The isNumber method:</p>
  
  <pre><code>public static boolean isNumber(String str)  {
          if((str.equals("-") &amp;&amp; fortegn == false)){ 
              fortegn = true; //fortegn is a variable that allows the number to have it's minus with it
              return true;
          }
              else if (str.equals("+") || str.equals("*") || str.equals("/") || str.equals("=")){ 
                      return false;
          } else if((str.equals(".") &amp;&amp; !fortegn) || (str.equals("0") &amp;&amp; !fortegn) || (str.equals("1") &amp;&amp; !fortegn) || (str.equals("2") &amp;&amp; !fortegn) || (str.equals("3") &amp;&amp; !fortegn) || (str.equals("4") &amp;&amp; !fortegn) ||
                  (str.equals("5") &amp;&amp; !fortegn) || (str.equals("6") &amp;&amp; !fortegn) || (str.equals("7") &amp;&amp; !fortegn) || (str.equals("8") &amp;&amp; !fortegn) || (str.equals("9") &amp;&amp; !fortegn)){
              return true;
          } else 
              return false;
      }
  </code></pre>
  
  <p><strong>EDIT:</strong>
  I have made a mini-program, as you can see below. However, the error does not occur in this example, so I am at a loss why it is happening. So I'm uploading a bigger part of my program. My output is 4, which is intended, and occurs. </p>
  
  <pre><code>import java.util.*;
  import java.lang.*;
  
  public class Eksempel {
  
  private static boolean fortegn = false;
  private static int  i = 1;
  static ArrayList xVærdi = new ArrayList();
  private static String ligning = "y=3-2+4x-6";
  
      public static boolean erTal(String str)  {
          if((str.equals("-") &amp;&amp; fortegn == false)){ //Variablen 'fortegn' sørger for at inkludere fortegnet af et tal, selvom det ikke er et tal. Dette sker kun en gang. 
              fortegn = true;
              return true;
          }
              else if (str.equals("+") || str.equals("*") || str.equals("/") || str.equals("=")){ 
                      return false;
          } else if((str.equals(".") &amp;&amp; !fortegn) || (str.equals("0") &amp;&amp; !fortegn) || (str.equals("1") &amp;&amp; !fortegn) || (str.equals("2") &amp;&amp; !fortegn) || (str.equals("3") &amp;&amp; !fortegn) || (str.equals("4") &amp;&amp; !fortegn) ||
                  (str.equals("5") &amp;&amp; !fortegn) || (str.equals("6") &amp;&amp; !fortegn) || (str.equals("7") &amp;&amp; !fortegn) || (str.equals("8") &amp;&amp; !fortegn) || (str.equals("9") &amp;&amp; !fortegn)){
              return true;
          } else //Kommer kun så langt hvis klienten har indtastet et bogstav andet end x. 
              return false;
      }
  public static void main(String[] args) {
  
      while(erTal(ligning.substring((ligning.indexOf("x")-i),(ligning.indexOf("x")-i+1))) == true){
                          xVærdi.add(ligning.substring((ligning.indexOf("x")-i),(ligning.indexOf("x")-i+1)));
                          i++;
                      }
  
      Collections.reverse(xVærdi);
      double xResult = Double.parseDouble(String.join("", xVærdi));
      System.out.println(xResult); //returns 4.0 which it should
  }}
  </code></pre>
  
  <p><strong>The longer code that doesn't work:</strong></p>
  
  <pre><code>import java.util.*;
  import java.lang.*;
  
  public class Server{
  private static String ligning = y=3x^3-2x^2+4x-6;
  private static String replacedLigning;
  private static int i = 1;
  static ArrayList xVærdi = new ArrayList();
  static ArrayList x2Værdi = new ArrayList();
  static ArrayList x3Værdi = new ArrayList();
  
  while(erTal(ligning.substring((ligning.indexOf("x^3")-i),
  (ligning.indexOf("x^3")-i+1))) == true){
  
  x3Værdi.add(ligning.substring((ligning.indexOf("x^3")-i),
  (ligning.indexOf("x^3")-i+1)));
                          i++;
                      }
  
                      replacedLigning = ligning.replace("x^3","");
                      while(erTal(replacedLigning.substring((replacedLigning.indexOf("x^2")-i),(replacedLigning.indexOf("x^2")-i+1))) == true){
                          x2Værdi.add(replacedLigning.substring((replacedLigning.indexOf("x^2")-i),(replacedLigning.indexOf("x^2")-i+1)));
                          i++;
                      }
  
                     System.out.println(replacedLigning); //gives: y=3-2x^2+4x-6
                      replacedLigning = replacedLigning.replace("x^2","");
  
                      System.out.println(replacedLigning); //gives y=3-2+4x-6
  
                      i=1;
                      while(erTal(replacedLigning.substring((replacedLigning.indexOf("x")-i),(replacedLigning.indexOf("x")-i+1))) == true){
                          xVærdi.add(replacedLigning.substring((replacedLigning.indexOf("x")-i),(replacedLigning.indexOf("x")-i+1)));
                          i++;
                      }
  
                      Collections.reverse(xVærdi);
                      Collections.reverse(x2Værdi);
                      Collections.reverse(x3Værdi);
  
                      System.out.println(xVærdi); **//Contains nothing: [] should contain [4]**
                      System.out.println(x2Værdi); **//Contains nothing: [] should contain [-2]**
                      System.out.println(x3Værdi); **//Contains [4, ., 6, -] should contain [3]**
  
                      double xResult = Double.parseDouble(String.join("", xVærdi)); //gets an error, because the arraylist is empty. 
                      double x2Result = Double.parseDouble(String.join("", x2Værdi));
                      double x3Result = Double.parseDouble(String.join("", x3Værdi));
  </code></pre>