forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
76 lines (65 loc) · 2.99 KB
/
Copy pathMain.java
File metadata and controls
76 lines (65 loc) · 2.99 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
package modern.challenge;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.List;
import java.util.Scanner;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) throws FileNotFoundException, IOException {
System.out.println("Read a file of double values:");
try (Scanner scanDoubles = new Scanner(
Path.of("doubles.txt"), StandardCharsets.UTF_8)) {
while (scanDoubles.hasNextDouble()) {
double number = scanDoubles.nextDouble();
System.out.println(number);
}
}
System.out.println("\nRead a file of people:");
try (Scanner scanPeople = new Scanner(Path.of("people.txt"), StandardCharsets.UTF_8)
.useDelimiter(";|,")) {
while (scanPeople.hasNextLine()) {
System.out.println("Name: " + scanPeople.next().trim());
System.out.println("Surname: " + scanPeople.next());
System.out.println("Age: " + scanPeople.nextInt());
System.out.println("City: " + scanPeople.next());
}
}
System.out.println("\nRead a file of people via tokens():");
try (Scanner scanPeople = new Scanner(Path.of("people.txt"), StandardCharsets.UTF_8)
.useDelimiter(";|,")) {
scanPeople.tokens().forEach(t -> System.out.println(t.trim()));
}
System.out.println("\nRead a file of people via tokens() and join the result via space:");
try (Scanner scanPeople = new Scanner(Path.of("people.txt"), StandardCharsets.UTF_8)
.useDelimiter(";|,")) {
String result = scanPeople.tokens()
.map(t -> t.trim())
.collect(Collectors.joining(" "));
System.out.println(result);
}
System.out.println("\nRead a file of people via findAll():");
try (Scanner sc = new Scanner(Path.of("people.txt"))) {
Pattern pattern = Pattern.compile("4[0-9]");
List<String> ages = sc.findAll(pattern)
.map(MatchResult::group)
.collect(Collectors.toList());
System.out.println("Ages: " + ages);
}
System.out.println("Enter a new person as (name, surname and age):\n");
Scanner scanConsole = new Scanner(System.in);
String name = scanConsole.nextLine();
String surname = scanConsole.nextLine();
int age = scanConsole.nextInt();
// an int cannot include "\n" so we need the next line just to consume the "\n"
scanConsole.nextLine();
String city = scanConsole.nextLine();
System.out.println("\nName: " + name);
System.out.println("Surname: " + surname);
System.out.println("Age: " + age);
System.out.println("City: " + city);
}
}