Skip to content

Easy challenges - part 2 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Aug 16, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Number Addition
  • Loading branch information
ardallie committed Aug 14, 2021
commit ecb4fa9491c04f77a328380de2131ce4d70e1a4f
46 changes: 46 additions & 0 deletions src/easy/NumberAddition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package easy;

/**
* Have the function NumberSearch(str) take the str parameter,
* search for all the numbers in the string, add them together,
* then return that final number.
* ---
* For example: if str is "88Hello 3World!" the output should be 91.
* You will have to differentiate between single digit numbers
* and multiple digit numbers like in the example above.
* So "55Hello" and "5Hello 5" should return two different answers.
* Each string will contain at least one letter or symbol.
*/
public class NumberAddition {

/**
* Number Addition function.
*
* @param str input string
* @return the final number
*/
private static int numberAddition(String str) {
String cleaned = str.replaceAll("[^0-9]", " ");
String[] splitNum = cleaned.split(" +");
int sum = 0;
for (String c : splitNum) {
if (!c.equals("")) {
sum += Integer.parseInt(c);
}
}
return sum;
}

/**
* Entry point.
*
* @param args command line arguments
*/
public static void main(String[] args) {
var result1 = numberAddition("Chillhouse Mix 2 (2001)");
System.out.println(result1);
var result2 = numberAddition("Cafe del Mar 5 (1998)");
System.out.println(result2);
}

}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy