Java > String-3 > sumNumbers (CodingBat Solution)

Problem:

Given a string, return the sum of the numbers appearing in the string, ignoring all other characters. A number is a series of 1 or more digit chars in a row. (Note: Character.isDigit(char) tests if a char is one of the chars '0', '1', .. '9'. Integer.parseInt(string) converts a string to an int.)

sumNumbers("abc123xyz") → 123
sumNumbers("aa11b33") → 44
sumNumbers("7 11") → 18


Solution:

public int sumNumbers(String str) {
  int len = str.length();
  int sum = 0;
  String tmp = "";
  
  for (int i = 0; i < len; i++) {
    if (Character.isDigit(str.charAt(i))) {
      if (i < len-1 && Character.isDigit(str.charAt(i+1))) {
        tmp += str.charAt(i);
      }
      else {
        tmp += str.charAt(i);
        sum += Integer.parseInt(tmp);
        tmp = "";
      }
        
    }
  }
  
  return sum;
}

20 comments:

  1. public int sumNumbers(String str) {
    int sum=0, num=0;
    for (char c : (str+".").toCharArray())
    if (Character.isDigit(c)){
    if (num > 0)
    num *= 10;
    num += (c - '0');
    } else {
    sum += num;
    num = 0;
    }
    return sum;
    }

    ReplyDelete
    Replies
    1. Sorry, that 2nd "if" is useless...

      public int sumNumbers(String str) {
      int sum=0, num=0;
      for (char c : (str+".").toCharArray())
      if (Character.isDigit(c))
      num = num*10 + (c - '0');
      else {
      sum += num;
      num = 0;
      }
      return sum;
      }

      Delete
  2. public int sumNumbers(String str) {
    int sum = 0;
    String str2 = str.replaceAll("[^0-9]+"," ");
    String arr[] = str2.trim().split(" ");
    for(int i = 0;i<arr.length;i++){
    if(arr[i]!=null && !arr[i].isEmpty()){
    sum += Integer.parseInt(arr[i]);
    }
    }
    return sum;
    }

    ReplyDelete
  3. public int sumNumbers(String str) {

    String tmp="";
    int sum=0;
    boolean status=false;

    for (int i = 0; i < str.length(); i++) {
    if (Character.isDigit(str.charAt(i))) {
    status = false;
    if (!status) {

    tmp = tmp + str.charAt(i);

    }
    }

    if (i == str.length() - 1 && Character.isDigit(str.charAt(i)))
    {
    sum = sum + Integer.parseInt(tmp);


    } else if (!Character.isDigit(str.charAt(i))) {
    if (!tmp.isEmpty())
    sum = sum + Integer.parseInt(tmp);

    status = true;
    tmp = "";
    }

    }
    return sum;
    }

    ReplyDelete
  4. public int sumNumbers(String str) {
    int sum=0;

    for(int i=0;i<str.length();i++){

    if(Character.isDigit(str.charAt(i))){

    for(int j=i+1;j<str.length()+1;j++){

    if(j==str.length()||!Character.isDigit(str.charAt(j))){
    sum+=Integer.parseInt(str.substring(i,j));
    i=j;
    break;
    }
    }
    }
    }

    return sum;
    }

    ReplyDelete
  5. public int sumNumbers(String str) {
    int sum = 0;
    for(int i = 0; i < str.length(); i++){
    if(Character.isDigit(str.charAt(i))){
    String num = "";
    int j;
    for(j = i; j < str.length() && Character.isDigit(str.charAt(j)); j++){
    num += str.charAt(j);
    }
    i = j--;
    sum += Integer.parseInt(num);
    }
    }
    return sum;
    }

    ReplyDelete
  6. public int sumNumbers(String str) {
    str = str.replaceAll("\\D", " ");
    str = str.replaceAll(" +", " ").trim();
    if(str.length()<1) return 0;
    String[] parts = str.split(" ");
    int result = 0;
    for(int i=0;i<parts.length;i++){
    result = result + Integer.parseInt(parts[i]);
    }
    return result;
    }

    ReplyDelete
  7. public int sumNumbers(String str) {
    int sum = 0;
    String tmp = "";
    for(int i=0; i<str.length(); i++){
    if(Character.isDigit(str.charAt(i))){
    tmp += str.charAt(i);
    if(i == str.length()-1 || !Character.isDigit(str.charAt(i+1))){
    sum += Integer.parseInt(tmp);
    tmp = "";
    }
    }
    }
    return sum;
    }

    ReplyDelete
  8. public int sumNumbers(String str) {
    return Arrays.stream(str.split("\\D+"))
    .filter(s -> !s.equals(""))
    .mapToInt(Integer::parseInt).sum();
    }

    ReplyDelete
  9. is it possible to do it with two for loops, using no parseInt?

    ReplyDelete
  10. public int sumNumbers(String str) {
    // Recursive Solution! //
    if (str.length() < 1)
    {
    return 0;
    }
    else
    {
    int count = 0;
    while (count < str.length() && Character.isDigit(str.charAt(count)))
    {
    count++;
    }
    if (count > 0)
    {
    return Integer.parseInt(str.substring(0, count)) + sumNumbers(str.substring(count));
    }
    else
    {
    return sumNumbers(str.substring(1));
    }
    }
    }

    ReplyDelete
  11. public int sumNumbers(String str) {
    int sum = 0, num = 0;
    for (int i = 0; i < str.length(); i++) {
    while (i < str.length() && Character.isDigit(str.charAt(i))) {
    num = (num * 10) + Integer.parseInt(str.substring(i, i + 1));
    i++;
    }
    sum += num;
    num = 0;
    }
    return sum;
    }

    ReplyDelete
  12. public int sumNumbers(String str) {
    int i = 0;
    int result = 0;
    int result2 = 0;
    int sum = 0;
    String n1 = "";
    for (i = 0; i < str.length(); i++) {
    if (Character.isDigit(str.charAt(i))) {
    n1+=str.charAt(i);
    result = Integer.parseInt(n1);
    }
    else if (!Character.isDigit(str.charAt(i)) ) {
    if(sum!=result+result2) {
    sum += result;
    n1 = "";
    result=0;
    }
    }
    if(i==str.length()-1 && !n1.isEmpty()){
    sum+=result;
    }
    }
    return sum;
    }

    ReplyDelete
    Replies
    1. Sorry, int result2 is not needed, i forgot to remove it of the code. Without it:

      public int sumNumbers(String str) {
      int i = 0;
      int result = 0;
      int sum = 0;
      String n1 = "";
      for (i = 0; i < str.length(); i++) {
      if (Character.isDigit(str.charAt(i))) {
      n1+=str.charAt(i);
      result = Integer.parseInt(n1);
      }
      else if (!Character.isDigit(str.charAt(i)) ) {
      if(sum!=result) {
      sum += result;
      n1 = "";
      result=0;
      }
      }
      if(i==str.length()-1 && !n1.isEmpty()){
      sum+=result;
      }
      }
      return sum;
      }

      Delete
  13. public int sumNumbers(String str) {
    str+="x";
    int number = 0;
    String digit = "";
    int newDigit = 0;
    for(int i=0; i<str.length()-1; i++){
    if(Character.isDigit(str.charAt(i))){
    digit+=str.charAt(i);
    if(!Character.isDigit(str.charAt(i+1))) {
    newDigit = Integer.parseInt(digit);
    number += newDigit;
    digit = "";
    }
    }
    }
    return number;
    }

    ReplyDelete
  14. public boolean has12(int[] nums) {
    boolean flag1 = false, flag2 = false;
    for(int i = 0; i < nums.length; i++){
    if(nums[i] == 1) flag1 = true;
    if(nums[i] == 2 && flag1) flag2 = true;
    if(flag1 && flag2) return true;
    }
    return false;
    }

    ReplyDelete
  15. public int sumNumbers(String str) {
    String a = "0";
    int b = 0;
    for(int i = 0; i < str.length(); i++){
    if(Character.isDigit(str.charAt(i))){
    a += str.substring(i, i + 1);
    if(i == str.length() - 1){
    b += Integer.parseInt(a);
    return b;
    }
    }else{
    b += Integer.parseInt(a);
    a = "0";
    }
    }
    return b;
    }

    ReplyDelete
  16. public static int sumNumbers(String str) {
    int sum=0;
    String str2=str.replaceAll("[^0-9]"," ");
    String []arr=str2.split(" ");
    for(int i=0; i<arr.length;i++) {
    if(!arr[i].isEmpty() ) {

    sum+=Integer.parseInt(arr[i]);
    }
    }
    return sum;
    }

    ReplyDelete