Java - 使用扫描仪从文本文件读取到二维字符数组

标签 java arrays java.util.scanner

我正在尝试将文本文件读入 30x30 字符数组。 Scanner 没有 nextChar 方法,所以我假设我将使用 next() ,然后将该行拆分为字符?不过,我坚持使用 3 个 for 循环来做到这一点,一个用于字符串,一个用于行,一个用于列。

这是到目前为止我的代码..

public static void main(String[] args) throws FileNotFoundException {
    final int cellSize = 30; // grid size
    char[][] chargrid = new char[cellSize][cellSize];
    File inputFile = new File("E:\\workspace\\Life2\\bin\\Sample input.txt");
    Scanner in = new Scanner(inputFile);

    char testchar;
    while(in.hasNext()){
    String s = in.next();
    for (int i=0; i<s.length();i++){
     testchar = s.charAt(i);

现在我要为数组行和列执行 2 个 for 语句,然后设置 chargrid[i][j] = testchar 吗?

如有任何帮助,我们将不胜感激。

最佳答案

据我在您的评论中看到,如果字符是“X”,您还希望有一个带有 boolean 值的二维数组,因此我在代码中填充了两个数组 - 一个实际上包含字符,另一个包含 true 或 false,取决于字符是否为“X”。还有一些 system.out 可以更好地理解它是如何工作的。我在出现时删除换行符('\n')(不知道你是否想要)

public static void main(String[] args) {
    final int cellSize = 30; // grid size
    char[][] chargrid = new char[cellSize][cellSize];
    boolean[][] chargridIsX = new boolean[cellSize][cellSize];
    File inputFile = new File("input.txt");
    FileInputStream is = null;
    try {
        is = new FileInputStream(inputFile);
        int n = -1;
        int rowCount = 0;
        int colCount = 0;
        while( (n=is.read()) !=-1){
            char readChar = (char) n;
            if(readChar!='\n'){//This removes the linebreaks - dont know if you want that
                chargrid[rowCount][colCount] = readChar;
                if(readChar=='X') { // Here actually set true or false if character is 'X'
                    chargridIsX[rowCount][colCount] = true;
                    System.out.println("row "+rowCount+" col "+colCount + " = " + readChar + " " + true);
                }else {
                    chargridIsX[rowCount][colCount] = false;
                    System.out.println("row "+rowCount+" col "+colCount + " = " + readChar+ " " + false);
                }
                if(rowCount++ >= cellSize-1){
                    System.out.println("new col");
                    rowCount = 0;
                    if(colCount++ >= cellSize-1){
                        //ARRAY IS FULL
                        System.out.println("full");
                        break;
                    }
                }
            }
        }

    } catch (FileNotFoundException e) {
        //could not get Inputstream from file
        e.printStackTrace();
    } catch (IOException e) {
        // problem with the inputstream while reading
        e.printStackTrace();
    }
顺便说一句。我正在从输入流中逐个字符地读取字符,而不是使用扫描仪,希望没关系 - 否则请告诉我

无论如何,这可能是件好事

关于Java - 使用扫描仪从文本文件读取到二维字符数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9606038/

相关文章:

java - Websphere 8.3 上 OFBIZ 的 TransactionManager

c - 树排序不排序我的数组

java - 数组等于忽略顺序

java - 创建一个 Java 程序来搜索文件中的特定单词

java - 如果扫描仪读取特定输入则中断

java - schema.org 在 Java 中反序列化

java - Spring Boot Job Scheduler fixedDelay 和 cron

java - Spring 不使用抽象父类(super class)将 DAO 注入(inject)到 JSF 托管 bean 中

javascript - 特定对象的 TypeScript 枚举

java - 使用 Scanner() 读取 CSV