-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegex1.java
More file actions
30 lines (24 loc) · 948 Bytes
/
Regex1.java
File metadata and controls
30 lines (24 loc) · 948 Bytes
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package devsought;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regex1 {
public static void main(String... args) {
String str = "John's end of term scores were as follows;Math 72%,English 65%,Science 85% and Commerce 45%. His performance was above average.";
Pattern pattern = Pattern.compile("\\d{1,3}");
Matcher matcher = pattern.matcher(str);
int sum = 0;
int count = 0;
while (matcher.find()) {
String subsequence = matcher.group();
sum += Double.valueOf(subsequence);
count++;
System.out.println(subsequence);
}
System.out.println("Total score=" + sum + " ,avg=" + (sum / count));
}
}