0

I'm trying to make a mini maze game. I have a text file which includes # for walls and whitespaces for grass. I'm having an issue reading the file and the lines atm. I tried using different methods but im not really familiar with reading files and how to read lines.

public class Map {

   private Scanner m;
   private Image wall,grass;
   private int lines;
   private char Map[][];

public Map(){

    ImageIcon img = new ImageIcon("/Users/parischrysanthou/Downloads/handpainted_wall2.jpg");
    wall = img.getImage();

    img = new ImageIcon(("/Users/parischrysanthou/Downloads/grass20.png"));
    grass = img.getImage();

    openFile();
    readFile();
    closeFile();
}

public int getLines() { return lines; }

public Image getGrass(){
    return grass;
}

public Image getWall() {
    return wall;
}

public char getMap(int x, int y){
    char index = Map[x][y];
    return index;
}

public void openFile(){
    try {
        File f = new File("/Users/parischrysanthou/Downloads/maze21-2.txt");
        Scanner m = new Scanner(f);
        while (m.hasNextLine()) {
            m.nextLine();
            lines++;
        }
        Map = new char [lines][lines];

    }
    catch(Exception e){
        System.out.println("Error loading file");
    }
}
public void readFile() {
    m = new Scanner("/Users/parischrysanthou/Downloads/maze21-2.txt");
    while (m.hasNextLine()) {
        for (int i = 0; i < lines; i++) {
            String s = m.nextLine();
            for (int j = 0; j < lines; j++) {
                Map[i][j] = s.charAt(j);

            }
        }
    }
}
public void closeFile(){
    m.close();
    }
}

Apparently my file is read as a string instead of reading the contents.I tried using BufferedReader but im unable to use hasNextLine. Any help is appreciated.

Edit: Sample of text file http://unload.io/rJ3OYFcpx.clean

Mortexe
  • 9
  • 3

0 Answers0