Skip to content

Commit ecb4fa9

Browse files
committed
Number Addition
1 parent 8b69a49 commit ecb4fa9

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/easy/NumberAddition.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package easy;
2+
3+
/**
4+
* Have the function NumberSearch(str) take the str parameter,
5+
* search for all the numbers in the string, add them together,
6+
* then return that final number.
7+
* ---
8+
* For example: if str is "88Hello 3World!" the output should be 91.
9+
* You will have to differentiate between single digit numbers
10+
* and multiple digit numbers like in the example above.
11+
* So "55Hello" and "5Hello 5" should return two different answers.
12+
* Each string will contain at least one letter or symbol.
13+
*/
14+
public class NumberAddition {
15+
16+
/**
17+
* Number Addition function.
18+
*
19+
* @param str input string
20+
* @return the final number
21+
*/
22+
private static int numberAddition(String str) {
23+
String cleaned = str.replaceAll("[^0-9]", " ");
24+
String[] splitNum = cleaned.split(" +");
25+
int sum = 0;
26+
for (String c : splitNum) {
27+
if (!c.equals("")) {
28+
sum += Integer.parseInt(c);
29+
}
30+
}
31+
return sum;
32+
}
33+
34+
/**
35+
* Entry point.
36+
*
37+
* @param args command line arguments
38+
*/
39+
public static void main(String[] args) {
40+
var result1 = numberAddition("Chillhouse Mix 2 (2001)");
41+
System.out.println(result1);
42+
var result2 = numberAddition("Cafe del Mar 5 (1998)");
43+
System.out.println(result2);
44+
}
45+
46+
}

0 commit comments

Comments
 (0)