【问题标题】:Java - Reading from text file using scanner into a 2D char arrayJava - 使用扫描仪从文本文件读取到二维字符数组
【发布时间】:2012-03-07 17:08:15
【问题描述】:

我正在尝试将文本文件读入 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 例如?

任何帮助将不胜感激。

【问题讨论】:

  • 您为什么要将文本文件读入 30x30 字符数组,为什么不直接读入 List&lt;String&gt;
  • 我真正想做的是有一个 30x30 布尔数组,如果 char='X' 则该单元格为真,但我什至无法将文本文件读入数组正确,更不用说这样做了。
  • 在这种情况下,我建议首先读取 List&lt;String&gt; 中的文件,然后检查此 List&lt;String&gt; 以填充您的 30x30 布尔数组。

标签: java arrays java.util.scanner


【解决方案1】:

据我在您的评论中看到的,如果字符是“X”,您还希望有一个带有布尔值的二维数组,所以我在代码中填充了两个数组 - 一个实际上是字符,一个是真或假,取决于字符是否为“X”。还有一些 system.outs 可以更好地理解它是如何工作的。出现时我正在删除换行符('\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();
    }

顺便说一句。我正在从输入流中读取字符而不是使用扫描仪,希望没问题 - 否则请告诉我

这可能是好事

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-13
    • 2020-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-19
    相关资源
    最近更新 更多