Skip to content

Commit ecb967e

Browse files
authored
Create JSoupValidateAndSum.java
1 parent 6b49159 commit ecb967e

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

JSoupValidateAndSum.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.zetcode;
2+
3+
import org.jsoup.Jsoup;
4+
import org.jsoup.nodes.Document;
5+
6+
import java.io.IOException;
7+
8+
// the program reads data from a web page, validates it and
9+
// calculates sum from them
10+
// uses custom validation method
11+
12+
public class JSoupValidateAndSum {
13+
14+
public static void main(String[] args) throws IOException {
15+
16+
String url = "http://test.webcode.me/data.txt";
17+
18+
Document doc = Jsoup.connect(url).get();
19+
20+
String content = doc.body().text();
21+
System.out.println(content);
22+
23+
String[] vals = content.split(",\\s+");
24+
25+
int sum = 0;
26+
27+
for (String val : vals) {
28+
29+
if (isNumeric(val)) {
30+
31+
sum += Integer.parseInt(val);
32+
}
33+
}
34+
35+
System.out.println(sum);
36+
}
37+
38+
public static boolean isNumeric(String val) {
39+
40+
int len = val.length();
41+
42+
for (int i = 0; i < len; i++) {
43+
44+
if (!Character.isDigit(val.charAt(i))) {
45+
46+
return false;
47+
}
48+
}
49+
50+
return true;
51+
}
52+
}

0 commit comments

Comments
 (0)