-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtxtFileReader.java
145 lines (107 loc) · 3.57 KB
/
txtFileReader.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class txtFileReader extends binFileReader{
public static int Columns(File file) throws IOException {
Scanner scanner = new Scanner(file);
String line = scanner.nextLine();
int Columns = line.length() - 1;//-2 for linux
scanner.close();
return Columns+1;
}
public static int Rows(File file) throws IOException {
Scanner scanner = new Scanner(file);
int Rows = 0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
Rows++;
}
scanner.close();
return Rows ;
}
public static int[][] TxtToInt(File file, int Columns, int Rows) throws IOException {
int[][] Lab = new int[Columns][Rows];
int y = 0;
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()){
String line = scanner.nextLine();
char[] chars = line.toCharArray();
for (int i = 0; i < Columns; i++) {
if (chars[i] == 'X') {
Lab[i][y] = 1;
} else if (chars[i] == ' ') {
Lab[i][y] = 0;
} else if (chars[i] == 'P') {
Lab[i][y] = 2;
} else if (chars[i] == 'K') {
Lab[i][y] = 3;
}
}
y++;
}
scanner.close();
return Lab;
}
public static int[] BeginningEnd(File file, int Columns, int Rows) throws IOException {
int[] BeginningEnd = new int[4];
int y = 0;
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
char[] chars = line.toCharArray();
for (int i = 0; i < Columns; i++) {
if (chars[i] == 'P') {
BeginningEnd[0] = i;
BeginningEnd[1] = y;
} else if (chars[i] == 'K') {
BeginningEnd[2] = i;
BeginningEnd[3] = y;
}
}
y++;
}
scanner.close();
return BeginningEnd;
}
/*public static void main(String[] args) {
System.out.println("working");
String FilePath = args[0];
File file = new File(FilePath);
int Columns = 0;
try {
Columns = Columns(file);
} catch (IOException e) {
throw new RuntimeException(e);
}
System.out.println("Columns: " + Columns);
int Rows = 0;
try {
Rows = Rows(file);
} catch (IOException e) {
throw new RuntimeException(e);
}
System.out.println("Rows: " + Rows);
int[][] Lab = null;
try {
Lab = TxtToInt(file,Columns,Rows);
} catch (IOException e) {
throw new RuntimeException(e);
}
for (int i = 0; i < Rows; i++) {
for (int j = 0; j < Columns; j++) {
System.out.print(+Lab[j][i]);
}
System.out.println();
}
int[] BeginEnd= null;
try {
BeginEnd = BeginningEnd(file, Columns, Rows);
} catch (IOException e) {
throw new RuntimeException(e);
}
System.out.println("BeginningEnd: " + BeginEnd[0]);
System.out.println("BeginningEnd: " + BeginEnd[1]);
System.out.println("BeginningEnd: " + BeginEnd[2]);
System.out.println("BeginningEnd: " + BeginEnd[3]);
}*/
}