title: Nested For-Loops - Separating String Elements with Array Objects
body: <p>I have a simple issue but it's been driving me nuts lately.
  I'm making a Chemistry program (Android app, to be more specific) and I have a method that separates elements that the user inputs. For example, if someone where to enter "FeZnKPb", it would be separated into "Fe", "Zn", "K", and "Pb".</p>
  
  <p>To do so, I'm using a few nested loops and variables to control it. I have all the elements defined in the Chem[] array. The separated elements are then stored in an array called savedChem[]. It basically just loops through all the element constants (Chem[]) and copies the name and formula of those elements into savedChem[] if it matches the input.</p>
  
  <p>Here is my code for this:</p>
  
  <pre><code>public void separateElements(String Input)
  {
      boolean found = false;
      int start = 0;
      int end = 2;
      int length = Input.length();
  
      while(length &gt;= end)
      {
          for(int x = 0; x &lt; numChemicals; x++)
          {
              if((end + 0) &gt; length)
              {
                  end += 5;
                  break;
              }
              if(Input.substring(start, end).equals(Chem[x].getFormula()))
              {
                  savedChem[numSaved].setName(Chem[x].getName());
                  savedChem[numSaved].setFormula(Chem[x].getFormula());
                  numSaved++;
                  start += 2;
                  end += 2;
                  found = true;
              }
              else
              {
                  found = false;
              }
          }
          if(!found)
          {
              start += 2;
              end += 2;
          }
      }
  }
  </code></pre>
  
  <p>My problem is that it only works with 2-character elements such as "Fe" or "Zn". I want it to also recognize others like "K".
  Another issue is that it sometimes skips some other elements. For example, if I were to input "FeZnHg" it would separate them into "Fe", "Zn", and "Hg". However, if I enter them in a different order like "ZnFeHg", it only detects "Zn" and "Hg" but not "Fe" for some reason.</p>
  
  <p>What other ways am I able to solve this to get it to work correctly?</p>