Java:文件读取问题

发布于 2024-08-22 18:18:29 字数 2945 浏览 2 评论 0原文

你会如何编写这段代码?

这个特殊的问题是关于一个迷宫游戏,它有一个占据者的数组列表,其中包括探险家(你)、怪物(触摸会杀死你)和宝藏。游戏使用这些居住者居住的方形物体块。我想要做的具体事情是文件读取,它可以导出迷宫的当前配置或将它们导入为 txt 文件。

规格: 首先读取迷宫的行和列以创建适当大小的 Square[][]。然后构建并读取所有方块/占用者。

对于正方形,迷宫将首先确定该线以“Square”开头。然后它将读取 Square 的行和列并使用该信息构造 Square 对象。最后,它将把 Scanner 的其余部分传递给 Square 的 toObject 方法,以便它可以初始化自身。

对于所有其他占用者,迷宫将确定它是什么类型的占用者,并使用仅采用迷宫的构造函数构造适当的对象。它不会从 Scanner 读取行或列,而只是将 Scanner 传递给新创建对象的 toObject 方法。

这是我到目前为止可能是错误的代码:

    public void readMazeFromFile(String fileName) throws IOException, FileNotFoundException, MazeReadException
   {
        Scanner fileSc = new Scanner(new File(fileName));
        String line = fileSc.nextLine(); //whats on the line, will be overwritten
        Scanner lineSc = new Scanner(line);
        String temp;
        lineSc.useDelimiter(",");
        int lineNum = 1; //every time you scan a line out, do lineNum++
        int r1, r2, r3, r4, c1, c2, c3, c4;

        rows = fileSc.nextInt();
        cols = fileSc.nextInt();
        Square hi = new Square(rows, cols);
        line = fileSc.nextLine();
        while ( line != null)
        {
            line = lineSc.nextLine();
            lineSc = new Scanner(line);

            if( lineSc.equals("Square"))
            {
                r1 = lineSc.nextInt();
                c1 = lineSc.nextInt(); 
                hi.toObject(lineSc);
            }
            if (lineSc.equals("Explorer"))
            {

                explorer.toObject(lineSc);
            }
            if (lineSc.equals("Treasure"))
            {
                Treasure.toObject(lineSc);
            }


            lineNum++;
        }

这是示例输出:

5,5
Square,0,0,true,false,false,true,true,true
Square,0,1,true,false,true,false,true,true
Square,0,2,true,false,true,false,false,false
Square,0,3,true,false,false,false,false,false
Square,0,4,true,true,false,false,false,false
Square,1,0,false,false,true,true,true,true
Square,1,1,true,false,true,false,false,false
Square,1,2,true,true,false,false,false,false
Square,1,3,false,true,false,true,false,false
Square,1,4,false,true,false,true,false,false
Square,2,0,true,false,false,true,false,false
Square,2,1,true,false,true,false,false,false
Square,2,2,false,true,false,false,false,false
Square,2,3,false,true,false,true,false,false
Square,2,4,false,true,false,true,false,false
Square,3,0,false,true,false,true,false,false
Square,3,1,true,false,false,true,false,false
Square,3,2,false,true,false,false,false,false
Square,3,3,false,true,true,true,false,false
Square,3,4,false,true,false,true,false,false
Square,4,0,false,true,true,true,false,false
Square,4,1,false,true,true,true,false,false
Square,4,2,false,false,true,true,false,false
Square,4,3,true,false,true,false,false,false
Square,4,4,false,true,true,false,false,false
Explorer,0,0,Scary Name
Treasure,4,4,true
Treasure,2,2,false
Monster,4,4
Monster,3,3

您会为本节编写什么?

How would you write this code?

This particular question is about a maze game that has an arraylist of occupants which are Explorers (you), Monsters (touching will kill you), and Treasures. The game uses blocks of square objects in which these occupants reside in. The particular thing I want to do is file reading which can export the current configuration of the maze or import them as a txt file.

The specs:
First read in the rows and cols of the Maze to create a Square[][] of the appropriate size. Then construct and read in all the Squares/Occupants.

For Squares, the Maze will first determine that the line starts with "Square". It will then read in the row and col of the Square and use that information to construct a Square object. Finally it will pass the rest of the Scanner to the Square's toObject method so it can initialize itself.

For all other Occupants, the Maze will determine what kind of Occupant it is and construct the appropriate object using the constructor that only takes a Maze. It will not read the row or the col from the Scanner, but simply pass the Scanner on to the toObject method of the newly created object.

This is code that I have so far which could be wrong:

    public void readMazeFromFile(String fileName) throws IOException, FileNotFoundException, MazeReadException
   {
        Scanner fileSc = new Scanner(new File(fileName));
        String line = fileSc.nextLine(); //whats on the line, will be overwritten
        Scanner lineSc = new Scanner(line);
        String temp;
        lineSc.useDelimiter(",");
        int lineNum = 1; //every time you scan a line out, do lineNum++
        int r1, r2, r3, r4, c1, c2, c3, c4;

        rows = fileSc.nextInt();
        cols = fileSc.nextInt();
        Square hi = new Square(rows, cols);
        line = fileSc.nextLine();
        while ( line != null)
        {
            line = lineSc.nextLine();
            lineSc = new Scanner(line);

            if( lineSc.equals("Square"))
            {
                r1 = lineSc.nextInt();
                c1 = lineSc.nextInt(); 
                hi.toObject(lineSc);
            }
            if (lineSc.equals("Explorer"))
            {

                explorer.toObject(lineSc);
            }
            if (lineSc.equals("Treasure"))
            {
                Treasure.toObject(lineSc);
            }


            lineNum++;
        }

Here is sample output:

5,5
Square,0,0,true,false,false,true,true,true
Square,0,1,true,false,true,false,true,true
Square,0,2,true,false,true,false,false,false
Square,0,3,true,false,false,false,false,false
Square,0,4,true,true,false,false,false,false
Square,1,0,false,false,true,true,true,true
Square,1,1,true,false,true,false,false,false
Square,1,2,true,true,false,false,false,false
Square,1,3,false,true,false,true,false,false
Square,1,4,false,true,false,true,false,false
Square,2,0,true,false,false,true,false,false
Square,2,1,true,false,true,false,false,false
Square,2,2,false,true,false,false,false,false
Square,2,3,false,true,false,true,false,false
Square,2,4,false,true,false,true,false,false
Square,3,0,false,true,false,true,false,false
Square,3,1,true,false,false,true,false,false
Square,3,2,false,true,false,false,false,false
Square,3,3,false,true,true,true,false,false
Square,3,4,false,true,false,true,false,false
Square,4,0,false,true,true,true,false,false
Square,4,1,false,true,true,true,false,false
Square,4,2,false,false,true,true,false,false
Square,4,3,true,false,true,false,false,false
Square,4,4,false,true,true,false,false,false
Explorer,0,0,Scary Name
Treasure,4,4,true
Treasure,2,2,false
Monster,4,4
Monster,3,3

What would you write for this section?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雄赳赳气昂昂 2024-08-29 18:18:29

这是一个你可以开始的骨架。在这里,您实际上只需要一个扫描仪即可;它可以完成您需要扫描输入并进行转换等的所有操作。您想使用它的 next()nextInt()nextBoolean() 和 nextLine() 方法。

    Scanner in = new Scanner(new File(filename));
    in.useDelimiter("\\s+|,");

    int rows = in.nextInt();
    int cols = in.nextInt();
    // construct the array

    while (in.hasNext()) {
        String type = in.next();
        int r = in.nextInt();
        int c = in.nextInt();
        // read more depending on type
    }

This is a skeleton that you can start with. You really don't need anything more than a Scanner here; it can do everything that you'd need to scan the input and do the conversion, etc. You want to use its next(), nextInt(), nextBoolean() and nextLine() methods.

    Scanner in = new Scanner(new File(filename));
    in.useDelimiter("\\s+|,");

    int rows = in.nextInt();
    int cols = in.nextInt();
    // construct the array

    while (in.hasNext()) {
        String type = in.next();
        int r = in.nextInt();
        int c = in.nextInt();
        // read more depending on type
    }
忘东忘西忘不掉你 2024-08-29 18:18:29

使用一个
FileReader
在文件上,然后附加一个
StreamTokenizer
这样做,效率要高得多:)

Use a
FileReader
on the file and then attach a
StreamTokenizer
to that, far more efficient :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文