Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 40635cb

Browse files
authored
Add files via upload
1 parent b001c21 commit 40635cb

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

‎FindSumOfNumbersApproachCharClass.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@ public class FindSumOfNumbersApproachCharClass {
77
public static void main(String[] args) {
88
String input ="asdf1qwer9as8d7";
99

10-
//using charArray operation
10+
//using Character class operation
1111
System.out.println("logic using Character class");
12-
String numberFromString = input.replaceAll("\\D", "");
13-
int sumAp1=0;
14-
char[] charArray = numberFromString.toCharArray();
15-
for (int i = 0; i < numberFromString.length(); i++) {
16-
char charAt = charArray[i];
17-
sumAp1 += Integer.parseInt(String.valueOf(charAt));
12+
int sum=0;
13+
for (int i = 0; i < input.length(); i++) {
14+
if (Character.isDigit(input.charAt(i))) {
15+
sum += Integer.parseInt(String.valueOf(input.charAt(i)))
16+
}
1817
}
19-
System.out.println("Sum of Integers "+sumAp1);
18+
System.out.println("Sum of Integers "+sum);
2019

2120
}
2221
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /