Hello, im trying to convert a .dat file (just ordinary text) to a 2d array. Ofcourse i can get codes from google for this, but id like to make it myself. Im quite far, it works if im not using any spaces in the map. But id like to have a map file so its easier to read for the mapmaker. It always returns array out of bounds. This is my map file:
0, 10, 10, 30, 0, 0, 0, 0,
0, 10, 20, 30, 0, 0, 0, 0,
0, 10, 20, 21, 40, 40, 40, 40,
0, 10, 20, 20, 20, 20, 20, 20,
0, 80, 50, 50, 50, 50, 50, 50,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
and my read map class:
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.ArrayList;
public class MapToArray {
public int height;
public int length;
String filename;
int[][] numbers;
LineNumberReader reader;
String lineRead = "";
public boolean error = false;
MapToArray(String filename) {
this.filename = filename;
try{
System.out.println("New map loaded!");
reader = new LineNumberReader(new FileReader(filename));
reader.close();
}catch (FileNotFoundException e){
System.out.println("File not found");
error = true;
}catch (IOException e){
System.out.println("Cant acces file!");
error = true;
}
}
public int[][] getArray() {
process();
return numbers;
}
public void getLength() {
int countChar = 0;
try {
reader = new LineNumberReader(new FileReader(filename));
lineRead = reader.readLine();
while (countChar < lineRead.length()) {
if (!(lineRead.charAt(countChar) == ','))
length++;
countChar++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void getHeigth() {
try {
reader = new LineNumberReader(new FileReader(filename));
while ((lineRead = reader.readLine()) != null) {
}
height = reader.getLineNumber();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void process() {
ArrayList<String> array = new ArrayList<String>();
getLength();
getHeigth();
try {
reader = new LineNumberReader(new FileReader(filename));
for (int i = 0; i < height; i++) {
array.add(reader.readLine());
}
} catch (IOException e) {
e.printStackTrace();
}
if (array.size() == 0) {
System.out.println("Empty map");
}
System.out.println("Length: " + length + " Heigth: " + height);
numbers = new int[length][height];
int r = 0;
for (String s : array) {
toArray(s, r);
r++;
}
}
private void toArray(String s, int row) {
System.out.print("String: "+s+"Row: "+row);
String match = ",";
String[] veld = s.split(match);
for (int i = 0; i < length; i++) {
if(!veld[i].matches(" ")){
try{
numbers[i][row] = Integer.parseInt(veld[i]);
}catch(NumberFormatException e){
System.out.println("Not a valid map!");
}
}
}
}
}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
at MapToArray.toArray(MapToArray.java:97)
at MapToArray.process(MapToArray.java:87)
at MapToArray.getArray(MapToArray.java:32)
at Board.<init>(Board.java:21)
at Main.<init>(Main.java:10)
at Main.main(Main.java:20)
Anybody sees my mistake here?