title: Parsing a string that represents a chemical reaction and verify if the reaction is possible
body: <p>I have to write a program that takes a user's chemical equation as an input, like 12 CO2 + 6 H2O -> 2 C6H12O6 + 12 O2, and watch if the amount of Atoms is on both sites the same. Is there any way to calculate and parse this easily?</p>
  
  <p>For example:</p>
  
  <p>12 CO2 + 6 H2O -> 2 C6H12O6 + 12 O2</p>
  
  <p>12*2+6*2 -> 2*6+2*12+2*6+12*2</p>
  
  <p>In this case there should be the Output "false".</p>
  
  <p>This is my code but it's actually is only to try out something:</p>
  
  <pre><code>public static void main(String[] args) {
      Scanner s = new Scanner(System.in);
      List&lt;String&gt; list = new ArrayList&lt;String&gt;();
      String input = "";
      while (!(input.equals("end"))) {
          input = s.nextLine();
          list.add(input);
      }
      list.remove(list.size() - 1);
      for (int i = 0; i &lt; list.size(); i++) {
          int before = 0;
          int after = 0;
          String string = list.get(i);
          string = besserUmwandeln(string);
          System.out.println(string);
      }
  }
  
  public static String besserUmwandeln(String string) {
      string = string.replace("-", "");
      string = string.trim().replaceAll(("\\s+"), " ");
      string = string.replace(' ', '*');
      StringBuilder builder = new StringBuilder(string);
      System.out.println(string);
      for (int k = 0; k &lt; builder.length(); k++) {
          if (Character.isUpperCase(builder.charAt(k))) {
              builder.setCharAt(k, ':');
          }
          if (Character.isLowerCase(builder.charAt(k))) {
              builder.setCharAt(k, '.');
          }
          if (Character.isDigit(builder.charAt(k))) {
          } else {
          }
      }
      for (int j = 0; j &lt; builder.length(); j++) {
          if (j &lt; builder.length() &amp;&amp; builder.charAt(j) == ':' &amp;&amp; builder.charAt(j + 1) == '.') {
              builder.deleteCharAt(j + 1);
          }
      }
      for (int i = 0; i &lt; builder.length(); i++) {
          if (i &lt; builder.length() - 1 &amp;&amp; builder.charAt(i) == ':' &amp;&amp; builder.charAt(i + 1) == ':') {
              builder.deleteCharAt(i);
          }
      }
      for (int i = 0; i &lt; builder.length(); i++) {
          if (i &lt; builder.length() - 1 &amp;&amp; builder.charAt(i) == '+' &amp;&amp; builder.charAt(i + 1) == '*') {
              builder.deleteCharAt(i + 1);
          }
      }
      for (int i = 0; i &lt; builder.length(); i++) {
          if (i &lt; builder.length() - 1 &amp;&amp; builder.charAt(i) == '*' &amp;&amp; builder.charAt(i + 1) == '+') {
              builder.deleteCharAt(i);
          }
      }
      for (int i = 0; i &lt; builder.length(); i++) {
          if (i &lt; builder.length() - 1 &amp;&amp; builder.charAt(i) == '*' &amp;&amp; builder.charAt(i + 1) == '&gt;') {
              builder.deleteCharAt(i);
          }
      }
      for (int i = 0; i &lt; builder.length(); i++) {
          if (i &lt; builder.length() - 1 &amp;&amp; builder.charAt(i) == '&gt;' &amp;&amp; builder.charAt(i + 1) == '*') {
              builder.deleteCharAt(i + 1);
          }
      }
      for (int i = 0; i &lt; builder.length(); i++) {
          if (i &lt; builder.length() - 1 &amp;&amp; builder.charAt(i) == '*' &amp;&amp; builder.charAt(i + 1) == ':') {
              builder.deleteCharAt(i + 1);
          }
      }
  
  
      return builder.toString();
  }
  </code></pre>