【问题标题】:Using StringTokenizer to convert a .txt file into a 2d array使用 StringTokenizer 将 .txt 文件转换为二维数组
【发布时间】:2021-09-29 16:08:42
【问题描述】:

文本文件(二维8.txt)。第一行是用','分隔的行和列

4,6
1,2,3,4,5,6
23,55,34,89,41,72
9,27,19,56,33,82
3,65,21,66,85,11

到目前为止我的代码。 使用 TextFileInput 类读取文件

import java.util.*;

public class Tokens {
   public static TextFileInput myFile;
   public static StringTokenizer myTokens;
   public static int[][] twodimarr;
   public static String line;
   
 
   public static void main(String[] args) {

      myFile = new TextFileInput("twodimension8.txt");
      line = myFile.readLine();
      System.out.println("The input line is "+line);

      myTokens = new StringTokenizer(line,",");

      int row = Integer.parseInt(myTokens.nextToken());
      int col = Integer.parseInt(myTokens.nextToken());

      twodimarr = new int[row][col];

      for (int i = 0; i < row; i++) {
          line = myFile.readLine();
          for (int j = 0; j < col; j++) {
             myTokens = new StringTokenizer(line, ",");
             twodimarr[i][j] = Integer.parseInt(myTokens.nextToken());
          }
      }

      for (int i=0; i<row; i++) {
          for (int j=0; j<col;j++)
             System.out.print(twodimarr[i][j]);
          System.out.println();
    }
         
   } //main
}

输出:

111111   
232323232323
999999     
333333  

问题似乎出在 for 循环中,它试图将其添加到二维数组中。我不确定如何解决这个问题。

【问题讨论】:

  • StringTokenizer 不推荐。这里当然不需要。还要避免添加很少或没有添加的第 3 方课程。 Scanner 在这里使用起来非常简单
  • 是的,我听说不推荐使用 StringTokenizer,但这是我的教授告诉我们使用的。
  • 令人担忧的迹象表明他的教材已经过时了多年

标签: java multidimensional-array tokenize


【解决方案1】:

实际的错误在这个块中:

      for (int i = 0; i < row; i++) {
          line = myFile.readLine();
          for (int j = 0; j < col; j++) {
             myTokens = new StringTokenizer(line, ",");
             twodimarr[i][j] = Integer.parseInt(myTokens.nextToken());
          }
      }

myTokens 应该每行只重新分配一次,而不是在每个单独的数字上。因此,改为将该位更改为:

      for (int i = 0; i < row; i++) {
          line = myFile.readLine();
          myTokens = new StringTokenizer(line, ",");
          for (int j = 0; j < col; j++) {
             twodimarr[i][j] = Integer.parseInt(myTokens.nextToken());
          }
      }

此外,将局部变量存储为静态类变量也不是一个好习惯。而不是做 public static TextFileInput myFile; 在类定义的开头,在main 的顶部做TextFileInput myFile = new TextFileInput("twodimension8.txt");。 (其他变量相同)。

【讨论】:

  • 啊,我确实尝试将 myTokens 每行放入一次,但没有用,但现在可以了。不错!
  • 如果这有助于解决您的问题,请将答案标记为已接受
【解决方案2】:

解决我上面提到的问题:

    public static int[][] loadArray(String path) throws IOException {
        int[][] result = null;
        try (Scanner s = new Scanner(new File(path))) {
            s.useDelimiter("[,\r\n]");
            int numRows = s.nextInt();
            int numCols = s.nextInt();
            result = new int[numRows][numCols];
            for (int row = 0; row < numRows; row++) {
                for (int col = 0; col < numCols; col++) {
                    result[row][col] = s.nextInt();
                }
            }
            return result;
        }
    }

【讨论】:

    猜你喜欢
    • 2015-06-02
    • 1970-01-01
    • 1970-01-01
    • 2021-06-19
    • 2014-04-06
    • 1970-01-01
    • 2019-01-23
    相关资源
    最近更新 更多