-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSumUpNumbers.java
31 lines (24 loc) · 991 Bytes
/
SumUpNumbers.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
CodeMaster has just returned from shopping. He scanned the check of the items he bought and gave the resulting string to Ratiorg to figure out the total number of purchased items. Since Ratiorg is a bot he is definitely going to automate it, so he needs a program that sums up all the numbers which appear in the given input.
Help Ratiorg by writing a function that returns the sum of numbers that appear in the given inputString.
Example
For inputString = "2 apples, 12 oranges", the output should be
sumUpNumbers(inputString) = 14.
*/
int sumUpNumbers(String inputString) {
int res=0;
String temp="";
for(int i=0;i<inputString.length();i++){
if(Pattern.matches("[0-9]",inputString.charAt(i)+"")){
temp=temp+inputString.charAt(i);
}
else{
if(temp.length()>0)
res+=Integer.parseInt(temp);
temp="";
}
}
if(temp.length()>0)
res+=Integer.parseInt(temp);
return res;
}