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