简体   繁体   中英

Storing text file as 2D character array

I have a text file:

##########
#.......*#
#.########
#........#
########.#
#@.......#
##########

How can I get this to be represented as a 2D character array like this:

char[][] maze = {{##########},
                 {#.......*#},
                 {#.########},
                 {#........#},
                 {########.#},
                 {#@.......#},
                 {##########}};

I'm also using a JFileChooser to get the text file and saving it as a java.io.File.

Here's what I have that finds the rows/columns and attempts to store the text file as a 2D char array.

import javax.swing.*;
import java.io.*;
import java.util.*;

/**
 * Created by marcusstone on 9/10/15.
 */
public class MazeReader {
    public static int colNum;
    public static int rowNum;
    public static int levels;
    public static File textFile;
    public char[][] maze = new char[rowNum][colNum];

    public int getRows() {
        return rowNum;
    }

    public int getCols() {
        return colNum;
    }

    public void loadFile() {
        JFileChooser chooseFile = new JFileChooser();
        if (chooseFile.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            textFile = chooseFile.getSelectedFile();
        } else {
            textFile = null;
        }

    }

    public void getMazeInfo() {
        try {
            Scanner myScanner = new Scanner(textFile);
            String firstLine = myScanner.nextLine();
            colNum = firstLine.length();
            rowNum = 1;
            levels = 1;
            while (myScanner.hasNextLine()) {

                String aLine = myScanner.nextLine();

                if (aLine.charAt(0) != '-') {
                    rowNum++;
                }
                if (aLine.charAt(0) == '-') {
                    levels++;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void getMazeCharArray() throws IOException {
        try {
            BufferedReader reader = new BufferedReader(new FileReader(textFile));
            String line = null;
            int x = 0, y = 0;
            //draw the maze
            for(int i = 0; i < rowNum; i ++){
                line = reader.readLine();
                System.out.println(line);
            }
            //add elements to the maze array
            for(int i = 0; i < rowNum; i++){
                for(int j = 0; j < colNum; j++){
                    maze[i]=line.toCharArray();
                }
            }
        }catch(IOException e) {
            e.printStackTrace();
        }
    }


    public static void main (String[] args) throws IOException {
        MazeReader myMazeReader = new MazeReader();
        myMazeReader.loadFile();
        myMazeReader.getMazeInfo();
        System.out.println(rowNum);
        System.out.println(colNum);
        myMazeReader.getMazeCharArray();

    } // end main
} // end class

First you put some tried material, but nevertheless here: First load file and read from that. Reading data, put into two -dimensional array, with x = y = your boundary,

char[x][y]   maze;

.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM