-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainLesson2_2.java
More file actions
83 lines (66 loc) · 2.3 KB
/
MainLesson2_2.java
File metadata and controls
83 lines (66 loc) · 2.3 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
package ru.MylearnCh1J1L1;
import java.nio.file.Paths;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.File;
public class MainLesson2_2 {
public static void main(String[] args) {
String currentPath = Paths.get("")
.toAbsolutePath()
.toString();
File textFile = new File(currentPath + "\\maintext.txt");
String formattedData = getResultData(parseFile(textFile));
System.out.print(formattedData);
}
public static String getResultData(String[][] data) {
String[] infoText = new String[] {
"Студент ",
" получил ",
" по предмету ",
};
StringBuilder sb = new StringBuilder();
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
sb.append(infoText[j]).append(data[i][j]);
}
sb.append(".\n");
}
return sb.toString();
}
public static String[][] parseFile(File file) {
int linesCount = countLines(file);
int currentIndex = 0;
String[][] parsedData = new String[linesCount][3];
try {
FileReader fReader = new FileReader(file);
BufferedReader bufRead = new BufferedReader(fReader);
String line = null;
for (int i = 0; i < linesCount; i++) {
line = bufRead.readLine();
for (String value : line.split(",")) {
parsedData[i][currentIndex] = value.split(":")[1]
.replace("\"", "");
currentIndex++;
}
currentIndex = 0;
}
bufRead.close();
}
catch (Exception e) {
System.out.println("Error");
}
return parsedData;
}
public static int countLines(File file) {
int count = 0;
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
while (reader.readLine() != null) count++;
reader.close();
}
catch (Exception e) {
System.out.println("Error");
}
return count;
}
}