I'm converting a text file into a two dimensional char array and getting indexes of specific chars.
I have this task where I need to read the map which is text file, and find the shortest route out. So far I converted the text file into a char array, but if I'm printing out the map on console, some of the chars are missing. For some reason I don't understand, I'm not getting correct location for the starting point.
My idea to use same logic I found about binary maze. If you read this and think I'm doing something completely stupid, then please let me know.
First, I created two methods to get how many rows and columns there are in the map:
private static int getRowsOfTheMap() throws IOException {
int rows = 0;
BufferedReader reader = getReader(filePath);
while (reader.readLine() != null) rows++;
reader.close();
return rows;
}
private static int getColumnsOfTheMap() throws IOException {
BufferedReader reader = getReader(filePath);
String firstRow = reader.readLine();
return firstRow.length();
}
Then, I created a method to get the map as 2D char array:
private static char[][] getMapFromFile() throws IOException {
char[][] map = new char[getRowsOfTheMap()][getColumnsOfTheMap()];
BufferedReader reader = getReader(filePath);
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[0].length; j++) {
map[i][j] = (char) reader.read();
}
}
return map;
}
And method for getting starting point:
private static int[] getStartingPoint(char[][] map) {
int[] startingPoint = {
-1,
-1
};
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
if (map[i][j] == 'X') {
startingPoint[0] = i;
startingPoint[1] = j;
}
}
}
return startingPoint;
}
Map from the text file and map output.
And with location expected is [1,2] actual is [1,3]
I was able to produce the correct map, and [1, 2]
as a starting-point with the following code; adapted from yours.
I created a Maze
class, with a map
method to populate the map
, rows
, and columns
fields.
Additionally, I created a startingPoint
method to return an int[]
of the starting point row and column indices.
Finally, I created an overridden toString
method for printing the maze.
You can utilize Arrays.binarySearch
to find X
.
class Maze {
File file;
char[][] map;
int rows, columns;
Maze(String path) throws IOException {
file = new File(path);
map();
}
void map() throws IOException {
StringBuilder string = new StringBuilder();
String line;
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String newline = System.lineSeparator();
rows = 0;
while ((line = reader.readLine()) != null) {
if (rows++ != 0) string.append(newline);
string.append(line);
columns = line.length();
}
}
BufferedReader reader = new BufferedReader(new StringReader(string.toString()));
map = new char[rows][columns];
int index = 0;
while ((line = reader.readLine()) != null)
map[index++] = line.toCharArray();
}
int[] startPoint() {
int index = 0;
int column;
for (char[] row : map) {
if ((column = Arrays.binarySearch(row, 'X')) > 0)
return new int[] { index, column };
index++;
}
return null;
}
@Override
public String toString() {
StringBuilder string = new StringBuilder();
String newline = System.lineSeparator();
for (char[] row : map)
string.append(row).append(newline);
return string.toString();
}
}
11111
1 X 1
1 1 1
1 1
111 1
[1, 2]