Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * @author Aamir khan
- */
- public class SumOfStrings {
- /**
- * @author Aamir khan
- */
- public static void main(String[] args) {
- //direct sum
- System.out.println(
- "123".chars().map(i -> Character.digit(i, 10)).sum()
- );
- //filter numeric values
- System.out.println(
- "123four".chars().filter(SumOfStrings::isNum).map(i -> Character.digit(i, 10)).sum()
- );
- //old school solution
- int sum = 0;
- for (char c : "four321".toCharArray()) {
- // if (c < 48 || c > 58) continue;
- if (!isNum(c)) continue;
- sum += Character.digit(c, 10);
- }
- System.out.println(sum);
- }
- private static boolean isNum(int c){
- return Character.isDigit(c);
- }
- }
Add Comment
Please, Sign In to add comment