-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReadTextFile.java
More file actions
93 lines (76 loc) · 2.94 KB
/
Copy pathReadTextFile.java
File metadata and controls
93 lines (76 loc) · 2.94 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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Scanner;
public class ReadTextFile {
public static void main (String[] args) {
List<Person> persons = loadPersons();
System.out.println("Number of persons loaded from CSV file: " + persons.size());
for (Person person : persons) {
System.out.println(person.getFullName() + ", age: " + person.getAge());
}
System.out.println("------------------------------------------------");
System.out.println("Number of persons with first name");
System.out.println(" * 'Matthew': " + countFirstName(persons, "Matthew"));
System.out.println(" * 'Charlotte': " + countFirstName(persons, "Charlotte"));
System.out.println("------------------------------------------------");
IntSummaryStatistics stats = getAgeStats(persons);
System.out.println("Minimum age: " + stats.getMin());
System.out.println("Maximum age: " + stats.getMax());
System.out.println("Average age: " + stats.getAverage());
}
public static int countFirstName(List<Person> persons, String firstName) {
return (int) persons.stream()
.filter(p -> p.getFirstName().equals(firstName))
.count();
}
public static IntSummaryStatistics getAgeStats(List<Person> persons) {
return persons.stream()
.mapToInt(Person::getAge)
.summaryStatistics();
}
public static List<Person> loadPersons() {
List<Person> list = new ArrayList<>();
File file = new File("resources/testdata.csv");
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
list.add(new Person(scanner.nextLine()));
}
} catch (FileNotFoundException ex) {
System.err.println("Could not find the file to be loaded");
}
return list;
}
public static class Person {
private int id;
private String firstName;
private String lastName;
private int age;
private String street;
private String city;
private String state;
private int zip;
public Person(String csvLine) {
String[] data = csvLine.split(",");
this.id = Integer.valueOf(data[0]);
this.firstName = data[1];
this.lastName = data[2];
this.age = Integer.valueOf(data[3]);
this.street = data[4];
this.city = data[5];
this.state = data[6];
this.zip = Integer.valueOf(data[7]);
}
public String getFirstName() {
return firstName;
}
public String getFullName() {
return firstName + " " + lastName;
}
public int getAge() {
return age;
}
}
}