title: Evaluate Polynomial String without using regex and API
body: <p>Given a polynomial with a single variable x, and the value of x as input, compute its value. Examples:</p>
  
  <pre><code>eval("-2x^3+10x-4x^2","3")=-60
  
  eval("x^3+x^2+x","6")=258
  </code></pre>
  
  <p>Description of issue: In this code I break the string into a substring whenever a +/- is encountered and pass the substring to a function which evaluates single term like "-2x^3". So my code for input = "-2x^3+10x-4x^2" calculates till "-2x^3+10x" only and skips "-4x^2" part.</p>
  
  <p>Can anyone please tell me whats wrong  here?</p>
  
  <pre><code>public class EvalPolyX2 {
  
      static String testcase1 = "-2x^3+10x-4x^2";
      static String testcase2 = "3";
  
      public static void main(String args[]){
          EvalPolyX2 testInstance = new EvalPolyX2();
          int result = testInstance.eval(testcase1,testcase2);
          System.out.println("Result : "+result);
      }
  
      public int eval(String str,String valx){
  
          int sum = 0;        
          String subStr = "";
          if(str.charAt(0) == '-')
          {
              int len = str.length();
              for (int i = 0; i &lt; len; i++)
              {
                  if(str.charAt(i) == '-' || str.charAt(i) == '+')
                  {                   
                      subStr = str.substring(0, i);
                      System.out.println("subStr="+subStr);
                      sum += evalSubPoly(subStr, valx);
                      str = str.substring(i);
                      len = str.length();
                      i = 0;
                  }               
              }
          }
          else if(str.charAt(0) != '-')
          {
              str = '+' + str;
              int len = str.length();
              for (int i = 0; i &lt; len; i++)
              {
                  if(str.charAt(i) == '-' || str.charAt(i) == '+')
                  {
                      subStr = str.substring(0, i);
                      System.out.println("subStr="+subStr);
                      sum += evalSubPoly(subStr, valx);
                      str = str.substring(i);
                      len = str.length();
                      i=0;
                  }
              }
          }
          return sum;
      }
  
      public int evalSubPoly(String poly,String valx){
          int len = poly.length();
          String num = "";
          String power = "";
          int exp = 0, coeff = 0;
  
          for(int i = 0; i &lt; len; i++)
          {
              if(poly.charAt(i) == 'x')
              {
                  num = poly.substring(0, i);
                  coeff = Integer.parseInt(num);                              
              }
              if(poly.charAt(i) == '^')
              {
                  power = poly.substring(i+1, len);
                  exp = Integer.parseInt(power);
              }                       
          }
  
          if(power.equals(""))
              exp = 1;
          System.out.println("coeff="+coeff);
  
          int sum = 1;
          int x = Integer.parseInt(valx);
  
          for (int i = 0; i &lt; exp; i++)
          {
              sum = sum*x;
          }
          System.out.println("sum="+sum);
          sum = sum*coeff;
  
          return sum;
      }
  }
  </code></pre>