Java 将文本文件存储为二维字符数组
我有一个文本文件:Java 将文本文件存储为二维字符数组,java,Java,我有一个文本文件: ########## #.......*# #.######## #........# ########.# #@.......# ########## 如何将其表示为二维字符数组,如下所示: char[][] maze = {{##########}, {#.......*#}, {#.########}, {#........#}, {
##########
#.......*#
#.########
#........#
########.#
#@.......#
##########
如何将其表示为二维字符数组,如下所示:
char[][] maze = {{##########},
{#.......*#},
{#.########},
{#........#},
{########.#},
{#@.......#},
{##########}};
我还使用JFileChooser获取文本文件并将其保存为java.io.file
下面是我找到的行/列,并尝试将文本文件存储为2D字符数组
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
import javax.swing.*;
导入java.io.*;
导入java.util.*;
/**
*由marcusstone于2015年9月10日创作。
*/
公共类MazeReader{
公共静态int colNum;
公共静态int rowNum;
公共静态int级别;
公共静态文件textFile;
公共字符[][]迷宫=新字符[rowNum][colNum];
公共int getRows(){
返回rowNum;
}
公共int getCols(){
返回colNum;
}
公共void加载文件(){
JFileChooser chooseFile=新的JFileChooser();
if(chooseFile.showOpenDialog(null)=JFileChooser.APPROVE\u选项){
textFile=chooseFile.getSelectedFile();
}否则{
textFile=null;
}
}
public void getMazeInfo(){
试一试{
Scanner myScanner=新扫描仪(文本文件);
字符串firstLine=myScanner.nextLine();
colNum=firstLine.length();
rowNum=1;
级别=1;
while(myScanner.hasNextLine()){
字符串aLine=myScanner.nextLine();
if(aLine.charAt(0)!='-'){
rowNum++;
}
if(aLine.charAt(0)='-'){
级别++;
}
}
}捕获(IOE异常){
e、 printStackTrace();
}
}
public void GetMazeCharray()引发IOException{
试一试{
BufferedReader reader=新的BufferedReader(新文件读取器(文本文件));
字符串行=null;
int x=0,y=0;
//画迷宫
for(int i=0;i
首先,您放置了一些经过尝试的材料,但在这里:
首先加载文件并从中读取。读取数据,放入二维数组,x=y=你的边界
char[x][y] maze;
.您尝试过的内容,请放在此处。在使用文件输入填充2D数组时,请使用嵌套循环。读取每一行,转换为char[],然后填充数组…我添加了尝试将文本文件存储为2D char数组的内容,但我遇到了越界异常,并且我确定了原因或存储是否正确。