【问题标题】:Java Maze from a file来自文件的 Java 迷宫
【发布时间】:2017-08-23 06:27:04
【问题描述】:

我从一个文件中得到了一个迷宫,我试着做这个 用一个程序编写一个练习4 类,该程序将这样一个迷宫文件读入一个二维布尔数组。然后在控制台上显示数组,每行一行。使用空白符号和#符号表示数组元素,以便控制台输出与迷宫文件具有相同的格式,请参见上面的示例。

    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;
                    }

                }
            }


        }
    }

我怎么得到这些错误我应该改变什么?任何我认为会有帮助的东西

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at assignmentce152.Exercise4.Mazes(Exercise4.java:28)
at assignmentce152.Exercise4.main(Exercise4.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at  sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

【问题讨论】:

标签: java arrays multidimensional-array error-handling computer-science


【解决方案1】:

您的程序似乎试图从扫描仪中读取两个整数,但没有找到两个整数。

让我猜猜:下面这行是错误的。

       Scanner lineOfFile = new Scanner(reader.readLine()); 

readLine() 读取文件的第一行。从这一行中,您可以创建一个可用于解析该行的扫描仪对象。例如,如果您的两个数字位于文件的前 行,它永远不会同时到达它们,并且会抛出 InputMismatchException

相反,您可以尝试丢弃 reader 并将您的扫描仪创建为

       Scanner lineOfFile = new Scanner(mazefile); 

这将允许您的扫描仪读取整个文本文件,而不仅仅是第一行。

如果这不能解决您的问题,我认为您需要编辑您的问题并添加文件外观示例。

【讨论】:

    猜你喜欢
    • 2020-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-22
    相关资源
    最近更新 更多