title: Write java program to sum of all integers from a random String
body: <p>I was asked this question in an interview recently (Java programming que)</p>
  
  <ul>
  <li>Return the sum of all integers from a random String. Continuous Integers must be considered as one number.</li>
  <li>If the input String does not have any Integers, return 0.</li>
  <li>You may ignore decimals, float, and other non-integer numbers</li>
  <li>@param str : Input String</li>
  <li><ul>
  <li>Given the following inputs, we expect the corresponding output:</li>
  </ul></li>
  <li>"-1a2b3c" => 4        (-1+2+3)</li>
  <li>"123ab!45c" => 168   (123+45)</li>
  <li>"abcdef" => 0        (no Integers in String)</li>
  <li>"0123.4" => 127      (0123+4)</li>
  <li>"dFD$#23+++12@#T1234;/.,10"  => 1279  (23+12+1234+10)</li>
  </ul>
  
  <h2>I was allowed to use any inbuilt functions as well</h2>
  
  <p>I wrote this code , however this seems little more complex. Can this code be done any other way?</p>
  
  <pre><code>public int convertString(String str){
  int num=0, currNo = 0;
  Boolean isNegative = false;
  
  for (int i=0 ; i&lt;str.length(); i++){
  Char t = str.charAt(i) ;
  
      if(t == ‘-‘){
         if(isNegative)
              num = num-currNo;
          else
              num = num + currNo;
  
         isNegative=true;
         currNo=0;
      }
      else if(((t-‘0’) &lt;= 9 &amp;&amp; (t-‘0’)&gt;=0)){
          currNo = (currNo*10) + t;
      }
      else {
  
              if(isNegative)
              num = num-currNo;
          else
              num = num + currNo;
  
          currNo=0;
          isNegative=false;
      }
  }
  
  
  if(isNegative)
              num = num-currNo;
          else
              num = num + currNo;                         
  isNegative=true;
  
  return num;
  }
  </code></pre>