首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有字符的java.util.scanner

带有字符的java.util.scanner
EN

Stack Overflow用户
提问于 2022-05-04 22:49:45
回答 1查看 76关注 0票数 -1

我正在尝试用面向对象的编程来创建一个迷宫搜索算法。我把迷宫存储在一个文件中,我希望文件在解题之前读取迷宫并打印出来(IK,其他方法比较容易,但我必须使用文件读取器)。但是,我的charAt函数不能工作,我也不知道如何使fileReader读取字符。如果有人能提供一个很好的解决方案:

代码语言:javascript
复制
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();
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-05 02:31:04

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

代码语言:javascript
复制
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中,这将是字符串的ArrayList (ArrayList<String>)。现在只需将数组列表中的每个元素打印到控制台窗口即可。我只是不知道,所以.我们假设与上面的类似,但是我们会把每一行转换成一个char[]数组,因为您似乎想用字符来完成这个任务。考虑到这个..。

需要做的是:

  1. 逐行读取文件。
  2. 将每一行读取并转换为char数组。
  3. 将其添加到mazeArray char中,将ArrayList (mazeArray)的内容添加到控制台窗口。H 215G 216

代码语言:javascript
复制
// 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.
} 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72120236

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档