You are on page 1of 2

import java.io.

LineNumberReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

abstract class Maze {


private char[][] maze;
private int row;
private int col;

Maze(char[][] maze, int row, int col){


this.maze = maze;
this.row = row;
this.col = col;
}

public void disPlayMazaArray(String fileName){


try{
LineNumberReader lineNumberReader = new LineNumberReader(new FileReader(fileName));
String line;

int m = 0;
while((line = lineNumberReader.readLine()) != null){
if(lineNumberReader.getLineNumber() == 1){
this.row = Integer.parseInt(String.valueOf(line.charAt(0)));
this.col = Integer.parseInt(String.valueOf(line.charAt(2)));

/*here assing the row and column */


this.maze = new char[row][col];
}
else{
int n = 0;
char singleArray[] = line.toCharArray();
for(int i = 0; i < row; i++){
this.maze[m][i] = singleArray[n];
//System.out.print(singleArray[n]+" ");
//System.out.print(maze[m][i]+" ");
n += 2;
}
m++;
}
}

/* display the maza array */


for(int p = 0; p < row; p++) {
for(int q = 0; q < col; q++){
System.out.print(maze[p][q]+" ");
}
System.out.print("\n");
}
}
catch(IOException ex){
System.out.println(ex);
}
}
}
public class Main extends Maze
{
public Main(){
super(null,0,0);
}
public static void main(String[] args) {

String fileName;
try{
Main ma = new Main();

System.out.println("========= WELCOME TO MAZA =========\n");


System.out.print("Enter the file name : ");
fileName = new Scanner(System.in).nextLine();

while(true){
File file = new File(fileName);
if(file.exists()){
break;
}
else{
System.out.println("There is no file with this name : "+fileName);
System.out.print("Please re-enter correct file name : "+fileName);
fileName = new Scanner(System.in).nextLine();
}
}
/*call the disPlayMazaArray array*/
ma.disPlayMazaArray(fileName);
}
catch(Exception ex){
System.out.println(ex);
}
}
}

You might also like