title: Take a string and divide it to 2 numbers and an operator
body: <p>I'm trying to do a project where the user types a simple mathematical question (like 14 + 3) and the program is going to do something (irrelevant). For that "something", I need the first number and the second number as two different integers, as well as the operator as a char. So I tried this code:</p>
  
  <pre><code>String str = "";
  str = scan.next();
  str.replaceAll(" ","");
  
  int ans = 0;
  int len = str.length();
  int pos = 0;
  
  String str1 = "";
  char opr = ' ';
  String str2 = "";
  
  int i;
  for (i = 0 ; i &lt; len ; i++)
  {
      if (str.charAt(i) &lt;= '9' &amp;&amp; str.charAt(i) &gt;= '0') 
      {
          pos = i;
          str1 = str1 + str.charAt(i);
      }
      else
          break;
  }
  
  opr = str.charAt(i);
  
  for (int j = i + 1 ; j &lt; len - i ; i++)
  {
      if (str.charAt(i) &lt;= '9' &amp;&amp; str.charAt(i) &gt;= '0') 
          str2 = str2 + str.charAt(i);
  }
  
  int n1 = Integer.parseInt(str1);
  int n2 = Integer.parseInt(str2);
  
  </code></pre>
  
  <p>I'm getting this problem: 
  Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 2 at java.lang.String.charAt(Unknown Source)</p>
  
  <p>Can anybody explain what is the problem with my code?
  By the way, I'm quite new to java and better with c++.</p>