Pretty much i have to read a txt file which contains a sample maze in it as so:

4
5
~~~00
00S~0
E~0~~
0~~~0

the first number is the number of rows and the second is number of columns. Im having trouble figuring out how to store just the "maze" into a 2d array based on the given column and row #. This is my code so far:

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class Driver {
    public static void main(String args[]){
        try{
            // Open the file that is the first 
            // command line parameter

            FileInputStream fstream = new FileInputStream(System.getProperty("user.dir")+ "\\Maze1.txt");

            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;

            while ((strLine = br.readLine()) != null)   {
              System.out.println (strLine);
            }
            //Close the input stream
            in.close();
            }
         catch (Exception e){
          //Catch exception if any
          System.err.println("Error: " + e.getMessage());
         }
    }
}

Here all im doing is just printing the contents of the file. Now i need help storing. HELP!

To solve your problem you need to consider:
How do the lines in the file relate to the rows in the array?
How do the characters in a line relate to the columns in the array?

This article has been dead for over six months. Start a new discussion instead.