java.util.scanner with chars [已关闭]

java.util.scanner with chars [closed]

提问人:Chris Jones 提问时间:5/5/2022 最后编辑:user207421Chris Jones 更新时间:5/5/2022 访问量:85

问:


编辑问题以包括所需的行为、特定问题或错误以及重现问题所需的最短代码。这将有助于其他人回答这个问题。

去年关闭。

我正在尝试使用面向对象编程创建迷宫查找算法。我将迷宫存储在一个文件中,我希望文件在解决迷宫之前读取迷宫并打印出来(IK,其他方式更容易,但我必须使用文件阅读器)。但是,我的 charAt 函数不起作用,我不确定如何使 fileReader 读取字符。如果有人能提供一个很棒的解决方案:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
    public static Maze maze;
    public static void main(String[] args) {
        ArrayList<char[]> mazeArray = new ArrayList<char[]>();
        try {
            File myMaze = new File("maze.txt");
            Scanner fileReader = new Scanner(myMaze);
            while (fileReader.hasNextLine()) {
                char[] mazeLayout = new char[(int) myMaze.length()];
                for (int i = 0; i < Integer.MAX_VALUE; i++){
                    for (int j = 1; j < myMaze.length(); j++){
                        mazeLayout[i] = fileReader.next().charAt(j);
                        mazeArray.add(mazeLayout);
                    }
                }
            }
            fileReader.close();
        } catch (FileNotFoundException e) {
            System.out.println("File does not exist.");
            e.printStackTrace();
        }
        maze = new Maze(mazeArray);
        maze.print();
    }
}
java 数组 arraylist char java.util.scanner

评论

1赞 user207421 5/5/2022
“不起作用”不是问题描述。再试一次。例外?堆栈跟踪?注意“我必须使用文件阅读器”:好吧,你不是,你正在使用 .Scanner

答:

0赞 DevilsHnd - 退した 5/5/2022 #1

拥有示例文件数据来查看正在读取的内容总是很好的,因为此时......我只会猜测。如果文件内容如下所示:

xx xxxxxxx
xx  xxxxxx
xxx xxxxxx
xxxx    xx
xxxxx x xx
xxxx  x  x
xxxx xxx x
xxxxxx   x
xxxxx  xxx
xxxxx xxxx

那么你就不需要 char[] 数组,你只需将每一行存储到 ArrayList 中,这将是 String () 的 ArrayList。现在只需将数组列表中的每个元素打印到控制台窗口即可。我只是不知道,所以......我们只是假设它如上所述,但我们将把每一行都转换为 char[] 数组,因为您似乎想用 Characters 来执行此操作。考虑到这一点......ArrayList<String>

需要做什么:

  1. 逐行读取文件。
  2. 将每一行读入并将其转换为数组。char
  3. 将此数组添加到 ArrayList。mazeArray
  4. 将 ArrayList () 的内容打印到控制台 窗。mazeArray
// ArrayList to hold the Maze from file.
ArrayList<char[]> mazeArray = new ArrayList<>();

// `Try With Resources` used here to auto-close reader and free resources.
try (Scanner fileReader = new Scanner(new File("maze.txt"))) {
            
    // String variable to hold each line read in
    String line;
    // Loop through the whole file line by line...
    while (fileReader.hasNextLine()) {
                
        // Task 1: Apply current line read to the `line` variable.
        line = fileReader.nextLine().trim(); // Remove any leading/trailing spaces as well.
                
        // Make sure the line isn't blank (if any). You don't want those.
        if (line.isEmpty()) {
            continue;  // Go to the top of loop and read in next line.
        }
                
        // Task #2: Convert the String in the variable `line` to a char[] array.
        char[] chars = line.toCharArray();
                
        // Task #3: Add the `chars` character array to the `mazeArray` ArrayList.
        mazeArray.add(chars);
    }
}
catch (FileNotFoundException ex) {
    Logger.getLogger(yourClassName.class.getName()).log(Level.SEVERE, null, ex);
}

/* Task #4: Print the maze contained within the `mazeArray` ArrayList
   to the Console Window.       */
// Outer loop to get each char[] array within the ArrayList.
for (char[] characters : mazeArray) {
    // Inner loop to get each character within the current char[] array.
    for (char c : characters) {
        System.out.print(c); //Print each character on the same console row.
    }
    System.out.println(); // Start a new line in console.
}