2015-02-09 19 views
1

我有一個字符串,它包含數字和字符以及特殊符號。但我需要計算一個字符串內的數字的總和。 假設我的字符串是字符串中的數字總和不正常

String input = "[email protected]_demo,9,8*#1234/add2doe"; 

結果應該是12 + 9 + 8 + 1234 + 2 = 1265但我的代碼我得到的結果作爲1 + 2 + 9 + 8 + 1 + 2 + 3 + 4 + 2 = 32。這是我的代碼

public class sumOfNumInString { 
    public static void main(String[] args) { 
     String input = "[email protected]_demo,9,8*#1234/add2doe"; 
     String output = ""; 
     int temp = input.length(); 
     for (int i = 0; i < temp; i++) { 
      Character c = input.charAt(i); 
      if (Character.isDigit(c)) { 
       output = output + c; 
      } 
     } 
     int result = Integer.parseInt(output); 
     System.out.println(result); 
     int num = result, sum = 0, r; 
     for (; num != 0; num = num/10) { 
      r = num % 10; 
      sum = sum + r; 
     } 
     System.out.println("Sum of digits of number: " + sum);//32 
     //Need output as :12+9+8+1234+2= 1265 
    } 
} 

回答

4

您需要確定要添加的數字序列,因爲此刻您將單個字符添加爲數字。匹配字符串與正則表達式來提取數字,然後解析並添加它們應該工作。

private static final Pattern pattern = Pattern.compile("\\d+"); 

public static int total(String input) { 
    Matcher matcher = pattern.matcher(input); 
    int total = 0; 

    while (matcher.find()) { 
     total += Integer.parseInt(matcher.group(0)); 
    } 

    return total; 
} 

當用輸入字符串調用時,返回1265。

感謝Francesco的提示!

+0

的圖案也可以被寫爲:'\\ d +'。然後while循環變爲'while(m.find())int current = Integer.parseInt(m.group()); \t \t \t total + = current; \t \t \t}' – 2015-02-09 11:11:29

+0

不錯的改進。 TBH我應該從一開始就尋找這個實現 - perl和類似的方法可以在一次調用中輕鬆地從字符串中獲取每個匹配。 – 2015-02-09 11:15:08

+0

最後要做的就是擺脫'current'變量。 – 2015-02-09 11:17:32

0

你應該加入你的連續數字。你可以用數字和操作構建一個字符串,然後分析它。

String res = ""; 
for (int i = 0; i < temp; i++) { 
     Character c = input.charAt(i); 
     if (Character.isDigit(c)) { 
      res += c.toString(); 
     }else{ 
      if(! (res.charAt(res.length() - 1).equals('+'))) res+= '+';     
      } 
    } 

現在你可以分析你的字符串資源與StringTokenizer的

StringTokenizer st = new StringTokenizer("+ "); 

int sum = 0; 
String number = ""; 


while(st.hasMoreTokens()) { 

number = st.nextToken(); 

sum += Integer.parseInt(number); 


} 
+0

不是一個好的答案,正則表達式可以避免所有這些。看看@Matthew Franglen的答案。 – 2015-02-09 11:27:51

+0

@FrancescoPalmiotto我試過:) – 2015-02-09 11:40:03

0

如果你不感興趣的正則表達式的解決方案,你可以簡單地遍歷所有字符,如果是數字將其添加到緩衝這將包含所有連續的數字。噹噹前字符不是數字時,我們需要重置緩衝區並處理其先前的爭用(包括其當前存儲的總和)。要做到這一點,你可以選擇連接你的字符串。最簡單和首選的方式就是使用StringBuilder。

public static int sumOfNumbers(String input) { 
    int sum = 0; 
    StringBuilder sb = new StringBuilder(); 

    for (char ch : input.toCharArray()) { 
     if (Character.isDigit(ch)) { 
      sb.append(ch); //add character to buffer -> "12"+'3'= "123" 
     } else if (sb.length() > 0) { 
      sum += Integer.parseInt(sb.toString());//increase sum based on current number 
      sb.delete(0, sb.length()); // reset buffer for new number 
     } 
    } 
    return sum; 
} 

用法

public static void main(String[] args) throws Throwable { 
    String input = "[email protected]_demo,9,8*#1234/add2doe"; 
    System.out.println("Sum of numbers: " + sumOfNumbers(input));// 1265 
} 

輸出:

Sum of numbers: 1265