-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathParsingExportData.java
More file actions
106 lines (77 loc) · 2.58 KB
/
ParsingExportData.java
File metadata and controls
106 lines (77 loc) · 2.58 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package week3;
import edu.duke.FileResource;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
/**
* Created by white on 11/17/15.
*/
public class ParsingExportData {
public static void main(String[] args) {
FileResource fr = new FileResource("../../resources/main/exports/exportdata.csv");
CSVParser parser = fr.getCSVParser();
//test 1
// countryInfo(parser, "Nauru");
// test 2
// listExportersTwoProducts(parser, "gold", "diamonds");
// test 3
// numberOfExporters(parser, "gold");
// test 4
bigExporters(parser, "$999,999,999,999");
}
/**
* Finds and returns a string of information about the country
*
* @param parser
*/
public static void countryInfo(CSVParser parser, String c) {
for (CSVRecord record : parser) {
String country = record.get("Country");
if (country.equals(c)) {
String exports = record.get("Exports");
String value = record.get("Value (dollars)");
System.out.println(country + ": " + exports + ", " + value);
}
}
}
/**
* Prints the names of all the countries that have
*
* @param parser
* @param exportItem1
* @param exportItem2
*/
public static void listExportersTwoProducts(CSVParser parser, String exportItem1, String exportItem2) {
for (CSVRecord record : parser) {
String country = record.get("Country");
String exports = record.get("Exports");
if (exports.contains(exportItem1) && exports.contains(exportItem2)) {
System.out.println(country);
}
}
}
/**
* This method returns the number of countries that export exportItem
*
* @param parser
* @param exportItem1
*/
public static void numberOfExporters(CSVParser parser, String exportItem1) {
int counter = 0;
for (CSVRecord record : parser) {
String exports = record.get("Exports");
if (exports.contains(exportItem1)) {
counter += 1;
}
}
System.out.println(counter);
}
public static void bigExporters(CSVParser parser, String amount) {
for (CSVRecord record : parser) {
String country = record.get("Country");
String value = record.get("Value (dollars)");
if (value.length() > amount.length()) {
System.out.println(country + ":" + value);
}
}
}
}