Search code examples
javafilefilereadermaze

How to read files in java?


I made this maze solver program and all of it works except for the "file reader". What I intend to do is that the file has a maze map with 1 as a wall, 0 as a pass you can go, 3 is start and 9 is the finish line. My school teacher told me that I can't manually enter the map into my program and I have to read the map file into my program through a function call file reader. The thing is that he never taught me how to read files. I've been searching the Internet for a long time and can't find anything that actually works. I'll show you guys my program so far with what I have done. In the code I wrote out what I find in the Internet about file reading and as you may know it's not working. The file reader function that I made is at the very end of my code. See if you can help me or not. :)

The file i'm trying to read is called maze1.txt and it have this in it: 10 12 3 1 1 0 0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 0 1 1 1 1 1 1 1 1 0 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1

this is my program:

import java.awt.event.*;
import java.util.Scanner;
import java.awt.*;
import java.io.FileReader;
import java.io.FileNotFoundException;

import javax.swing.*;
public class Mazegraphic extends JPanel implements  ActionListener,KeyListener{
static char comefrom;
static int width=1000,height=1000;  
static int originalrow;
static int originalcolumn;
int row,column;
static int[][] originalmaze;

static int[][] Maze= new int [100][100];

static int x=0,y=0;
static int xx=0, yy=0;
static int xend=0, yend=0;
static boolean passagepoint = true;
static boolean playmaze = true;
static boolean gogogo= false;
static JFrame f; 

public void init (){
    this.addKeyListener(this);  
    setFocusable(true);  
//  input();


    row= originalrow+2;
    column= originalcolumn+2;
    comefrom = 'n';

    // add imaginary wall
    for (int a=0;a<row; a++){
        Maze[a][0] = 1;
        Maze[a][column-1] = 1;
    }
    for (int b=0; b<column; b++){
        Maze[0][b] =1;
        Maze[row-1][b] = 1;
    }
    for (int a=0;a<originalrow; a++){ 
        for (int b=0; b<originalcolumn; b++){
            Maze[a+1][b+1]=originalmaze[a][b];
            }
        }



    for (int a=0;a<row; a++){ //find starting point
        for (int b=0; b<column; b++){
            if (Maze[a][b]==3){
                x=a;y=b;

            }
        }
    }

    for (int a=0;a<row; a++){ //find ending point
        for (int b=0; b<column; b++){
            if (Maze[a][b]==9){
                xend=a;yend=b;
            }
        }
    }
    if (xend==0 && yend==0) playmaze = false;
}
public void paintComponent (Graphics g){
    int i=0,j=0;
    for (i=0;i<originalrow;i++){

        for (j=0;j<originalcolumn;j++){
            if (Maze[i+1][j+1]==1){
                g.setColor(Color.blue);
                g.fillRect(j*30+30,i*30+30,30,30);
            }
            if (Maze[i+1][j+1]==0){
                g.setColor(Color.white);
                g.fillRect(j*30+30,i*30+30, 30, 30);
            }
            if (Maze[i+1][j+1]==3){
                g.setColor(Color.yellow);
                g.fillRect(j*30+30,i*30+30, 30, 30);
            }
            if (Maze[i+1][j+1]==9){
                g.setColor(Color.green);
                g.fillRect(j*30+30,i*30+30, 30, 30);
            }
            if (Maze[i+1][j+1]==6){
                g.setColor(Color.yellow);
                g.fillRect(j*30+30,i*30+30, 30, 30);
            }

        }


        if (playmaze==false){
            if (xend==0 && yend==0){
            g.setColor(Color.red);
            g.setFont(new Font("Rosewood Std Regular", Font.PLAIN, 100)); 
            g.drawString("Can not reach finish",150,750);
            g.drawString("point!!!",500,900);
            }
            else {
                g.setColor(Color.green);
                g.setFont(new Font("Rosewood Std Regular", Font.PLAIN, 100)); 
                g.drawString("You reach finish point!!!",50,750);
            }
        }

    }

}
public static void main(String [] args){
    Mazegraphic m = new Mazegraphic();
    f=new JFrame();
    m.init(); 
    f.setBackground(Color.white);    
    f.add(m);
    f.setSize(width+1000,height+500);
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.repaint();  
    Timer t=new Timer(20,m);       
    t.start();
}


public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

}


public void keyPressed(KeyEvent e) {
    // TODO Auto-generated method stub
    if (e.getKeyCode()==KeyEvent.VK_SPACE){
        gogogo=true;
    repaint();
    }
    repaint();
}


public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub

}


public void actionPerformed(ActionEvent e) {

    if (gogogo==true && playmaze== true) {


        while (Maze[x][y]!=9) {
            playmaze = true;


            if ((comefrom=='n')&&(Maze[x][y-1]!=1)){
                comefrom='e';
                y=y-1;
                if (Maze[x][y]==9) {
                    playmaze=false;
                    break;
                }
                Maze[x][y]=6;

            }
            if (comefrom=='w'&&Maze[x+1][y]!=1){
                comefrom='n';
                x=x+1;
                if (Maze[x][y]==9) {
                    playmaze=false;
                    break;
                }
                Maze[x][y]=6;

            }
            if (comefrom=='s'&&Maze[x][y+1]!=1){
                comefrom='w';
                y=y+1;
                if (Maze[x][y]==9) {
                    playmaze=false;
                    break;
                }
                Maze[x][y]=6;

            }
            if (comefrom=='e'&&Maze[x-1][y]!=1){
                comefrom='s';
                x=x-1;
                if (Maze[x][y]==9) {
                    playmaze=false;
                    break;
                }
                Maze[x][y]=6;

            }
            if (checknext()==false){ // change direction
                switch (comefrom) {
                case 's':comefrom='e';
                         break;
                case 'w':comefrom='s';
                        break;
                case 'n':comefrom='w';
                        break;
                case 'e':comefrom='n';
                        break;
                }
            /*  if (comefrom=='s'){
                    comefrom='e';
                }
                if (comefrom=='w'){
                    comefrom='s';
                }
                if (comefrom=='n'){
                    comefrom='w';
                }
                if (comefrom=='e'){
                    comefrom='n';
                }*/
            }
            else {
                if (Maze[x][y]==9) {
                    Maze[x][y]=6;
                    playmaze=false;
                    break;      
                    }
                else {
                        Maze[x][y]=6;
                        playmaze = true;
                }


                /*          for (int i=0;i<Maze.length; i++){

                for (int j=0; j<Maze[i].length; j++){

                    System.out.print( Maze[i][j] );

                }

                System.out.println();

            }

            System.out.println( );
                 */

                repaint();
            }
        }
        playmaze =false;

        repaint();
    }
}
public static boolean checknext(){
    if (comefrom=='n') {
        if(Maze[x][y-1]==1){
            if (Maze[x+1][y]!=1){
                x=x+1;
                return true;
            }
            else {
                return false;
            }
        }
    }
    if (comefrom=='e') {
        if(Maze[x-1][y]==1){
            if (Maze[x][y-1]!=1){
                y=y-1;
                return true;
            }
            else {
                return false;
            }
        }
    }
    if (comefrom=='s') {
        if(Maze[x][y+1]==1){
            if (Maze[x-1][y]!=1){
                x=x-1;
                return true;
            }
            else {
                return false;
            }
        }
    }
    if (comefrom=='w') {
        if(Maze[x+1][y]==1){
            if (Maze[x][y+1]!=1){
                y=y+1;
                return true;
            }
            else {
                return false;
            }
        }
    }
    return false;
}

public static void input(){

    try{
        FileReader Afr=new FileReader("maze1.txt"); //this is my file reader and it is not working
        Scanner input = new Scanner(Afr);
        originalrow = input.nextInt();
        originalcolumn = input.nextInt();
        originalmaze = new int [originalrow][originalcolumn];
        for (int a=0; a<originalrow;a++){
            for(int b=0;b<originalcolumn;b++){
                originalmaze[a][b]=input.nextInt();
            }
        }
    }
    catch(FileNotFoundException e){
        System.err.println("file not found");
    }
}

}

Solution

  • Here is a perfect example you should be able to base yours off of. This is reading lines of a file one by one. if you want them all together you can add them one by one to an array of strings or however you'd like. FileReader will read a file character by character so it is usually wrapped in a BufferedFileReader so that you can deal with it line by line or however you'd like

    // The name of the file to open.
        String fileName = "temp.txt";
    
        // This will reference one line at a time
        String line = null;
    
        try {
            // FileReader reads text files in the default encoding.
            FileReader fileReader = 
                new FileReader(fileName);
    
            // Always wrap FileReader in BufferedReader.
            BufferedReader bufferedReader = 
                new BufferedReader(fileReader);
    
            while((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
            }    
    
            // Always close files.
            bufferedReader.close();            
        }