String-3 > sumNumbers

String-3 > sumNumbers

public int sumNumbers(String str) {
  return sumNumbersHelper(str,0);
}

public int sumNumbersHelper(String str, int index){
  if (index >= str.length()) return 0;
  
  int currentInt = 0;
  if(Character.isDigit(str.charAt(index))){
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append(str.charAt(index));
    index++;
    while(index < str.length() && Character.isDigit(str.charAt(index))){
      stringBuilder.append(str.charAt(index));
      index++;
    }
    currentInt = Integer.parseInt(stringBuilder.toString());
  } 
  return currentInt +sumNumbersHelper(str, index+1);
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s