-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileReader.java
116 lines (109 loc) · 3.76 KB
/
FileReader.java
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
107
108
109
110
111
112
113
114
115
116
package com.elena.hashcode;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class FileReader {
public FileReader(String path) {
this.path = path;
}
public HashMap<String, List<String>> readHorizontal() throws FileNotFoundException {
HashMap<String, List<String>> images = new HashMap<>();
File file = new File(this.path);
Scanner scanner = new Scanner(file);
// Avoid first line
int nOfPhotos = scanner.nextInt();
int i = 0;
while (scanner.hasNext()) {
String str = scanner.next() + scanner.nextLine();
if (str.charAt(0) == 'V') {
i++;
continue;
}
String[] splitted = str.split(" ");
for (int j = 2; j < splitted.length; j++) {
String tag = splitted[j];
if (images.containsKey(tag)) {
images.get(tag).add(i+"");
}
else {
images.put(tag, new ArrayList<>());
images.get(tag).add(i+"");
}
}
i++;
}
return images;
}
public HashMap<String, List<String>> readVertical() throws FileNotFoundException {
HashMap<String, List<String>> images = new HashMap<>();
File file = new File(this.path);
Scanner scanner = new Scanner(file);
// Avoid first line
int nOfPhotos = scanner.nextInt();
int i = 0;
while (scanner.hasNext()) {
String str = scanner.next() + scanner.nextLine();
if (str.charAt(0) == 'H') {
i++;
continue;
}
String[] splitted = str.split(" ");
for (int j = 2; j < splitted.length; j++) {
String tag = splitted[j];
if (images.containsKey(tag)) {
images.get(tag).add(i+"");
}
else {
images.put(tag, new ArrayList<>());
images.get(tag).add(i+"");
}
}
i++;
}
return images;
}
public HashMap<String, List<String>> readPhotos() throws FileNotFoundException {
HashMap<String, List<String>> images = new HashMap<>();
File file = new File(this.path);
Scanner scanner = new Scanner(file);
// Avoid first line
int nOfPhotos = scanner.nextInt();
System.out.println(nOfPhotos + "");
int i = 0;
while (scanner.hasNext()) {
String str = scanner.next() + scanner.nextLine();
images.put(i+"", new ArrayList<>());
String[] splitted = str.split(" ");
for (int j = 2; j < splitted.length; j++) {
String tag = splitted[j];
images.get(i+"").add(tag);
}
i++;
}
return images;
}
public HashMap<String, List<String>> readVerticalPhotos() throws FileNotFoundException {
HashMap<String, List<String>> images = new HashMap<>();
File file = new File(this.path);
Scanner scanner = new Scanner(file);
// Avoid first line
int nOfPhotos = scanner.nextInt();
int i = 0;
while (scanner.hasNext()) {
String str = scanner.next() + scanner.nextLine();
if (str.charAt(0) == 'H') {
i++;
continue;
}
images.put(i+"", new ArrayList<>());
String[] splitted = str.split(" ");
for (int j = 2; j < splitted.length; j++) {
String tag = splitted[j];
images.get(i+"").add(tag);
}
i++;
}
return images;
}
private final String path;
}