文件中的Java迷宫
我从一个文件中得到了一个迷宫,我试着让它写一个类Exercise4,用一个程序将这样一个迷宫文件读入一个二维布尔数组。然后在控制台上显示数组,每行显示一行。使用空白符号和#-符号表示数组元素,以便控制台输出具有与迷宫文件相同的格式,请参见上面的示例文件中的Java迷宫,java,arrays,multidimensional-array,error-handling,computer-science,Java,Arrays,Multidimensional Array,Error Handling,Computer Science,我从一个文件中得到了一个迷宫,我试着让它写一个类Exercise4,用一个程序将这样一个迷宫文件读入一个二维布尔数组。然后在控制台上显示数组,每行显示一行。使用空白符号和#-符号表示数组元素,以便控制台输出具有与迷宫文件相同的格式,请参见上面的示例 package assignmentce152; import java.io.BufferedReader; import java.io.File; import java.io.FileReader;
package assignmentce152;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.io.FileNotFoundException;
/**
* Created by ak on 29/03/2017.
*/
public class Exercise4 {
public static void main(String[] args) throws IOException {
Mazes();
}
private static char[][] maze = null;
private static int rows = 0;
private static int cols = 0;
private static int xStart = 0;
private static int yStart = 0;
public static void Mazes() throws IOException {
File mazefile = new File("C:/Users/IdeaProjects/Assignment152/data/maze21.txt");
BufferedReader reader = new BufferedReader(new FileReader(mazefile));
Scanner lineOfFile = new Scanner(reader.readLine());
rows = lineOfFile.nextInt(); //get the number of rows of the maze
cols = lineOfFile.nextInt(); // get the number of columns of the maze
maze = new char[rows][cols]; //create a char array of the proper size
//For loops to iterate the rows and col to find the start/enterance of the maze as it pertains to the first char in the row
for (int y = 0; y < cols; y++) {
lineOfFile = new Scanner(reader.readLine());
for (int x = 0; x < rows; x++) {
char start = lineOfFile.next().charAt(0);
maze[x][y] = start;
//statement to set the starting coorinates for the maze
if (start == '.') {
xStart = x;
yStart = y;
}
}
}
}
}
您的程序似乎试图从扫描仪中读取两个整数,但在那里找不到两个整数 让我猜猜:下面这行是错的
Scanner lineOfFile = new Scanner(reader.readLine());
readLine()
读取文件的第一行。从这一行创建一个scanner对象,该对象可用于分析该行。例如,如果您的两个数字位于文件的前两行,它将永远无法同时到达这两行,并将抛出一个inputmaschException
相反,您可以尝试放弃读卡器
,将扫描仪创建为
Scanner lineOfFile = new Scanner(mazefile);
这将允许扫描仪读取整个文本文件,而不仅仅是第一行
如果这不能解决您的问题,我相信您需要编辑您的问题并添加文件外观的示例。您的程序似乎试图从扫描仪中读取两个整数,但在那里找不到两个整数 让我猜猜:下面这行是错的
Scanner lineOfFile = new Scanner(reader.readLine());
readLine()
读取文件的第一行。从这一行创建一个scanner对象,该对象可用于分析该行。例如,如果您的两个数字位于文件的前两行,它将永远无法同时到达这两行,并将抛出一个inputmaschException
相反,您可以尝试放弃读卡器
,将扫描仪创建为
Scanner lineOfFile = new Scanner(mazefile);
这将允许扫描仪读取整个文本文件,而不仅仅是第一行
如果这不能解决您的问题,我相信您需要编辑您的问题,并添加文件外观的示例。老实说,我不知道如何在我自己的程序中修复它。因为不是很相似,答案有用吗?如果是的话,请考虑接受。如果有任何不清楚或不充分的地方,请随时在评论中跟进。老实说,我不知道如何在我自己的程序中修复它。因为不是很相似,答案有用吗?如果是的话,请考虑接受。如果有任何不清楚或不充分的地方,请随时在评论中跟进。