【问题标题】:Reading from a .txt into a 2D array从 .txt 读取到二维数组
【发布时间】:2012-11-21 19:14:15
【问题描述】:

对不起,如果我的代码看起来很糟糕,我在编程方面没有那么经验丰富。我需要以以下格式从 .txt 传输文本:Date-Name-Address-etc..

我正在读取文件,然后使用 String.split("-") 拆分字符串。我遇到了循环问题。

    try{
        File file = new File("testwrite.txt");
        Scanner scan = new Scanner(file);
        String[] test = scan.nextLine().split("-");
        while(r<100){
            while(c<6){
                data[r][c] = test[c];
                test = scan.nextLine().split("-");
                c++;
            }
            r++;
            c = 0 ;
        }
        System.out.println(data[1][5]);
    }catch(Exception e){
        System.out.println("Error: " + e.getMessage());
    }

【问题讨论】:

  • System.out.println(data[1][5]) - 用于测试目的。
  • 看起来for-loop 更适合这个。
  • 首先是第8行,test = scan.nextLine().split("-"); test是一个字符串数组,需要指定索引。
  • 如果您提供更多详细信息,我很乐意提供帮助。请编辑您的问题。

标签: java arrays file multidimensional-array


【解决方案1】:

二维数组就是“数组的数组”,所以可以直接使用splitresult来存储一行的数据。

            File file = new File("testwrite.txt");
            Scanner scanner = new Scanner(file);
            final int maxLines = 100;
            String[][] resultArray = new String[maxLines][];
            int linesCounter = 0;
            while (scanner.hasNextLine() && linesCounter < maxLines) {
                resultArray[linesCounter] = scanner.nextLine().split("-");
                linesCounter++;
            }

【讨论】:

    【解决方案2】:

    您似乎过于频繁地调用 scan.nextLine()。每次调用 scan.nextLine() 时,它都会使 Scanner 超过当前行。假设您的文件有 100 行,每行有 6 个“条目”(由“-”分隔),我会将 test = scan.nextLine().split("-"); 移动到 while 循环的末尾(但仍在循环内),以便每行调用一次。

    编辑...

    建议的解决方案: 给定表单中的文件,

    a-b-c-x-y-z

    a-b-c-x-y-z ...(总共 100 次)

    使用此代码:

    try{
        File file = new File("testwrite.txt");
        Scanner scan = new Scanner(file);
        String[] test = scan.nextLine().split("-");
        while(r<100){
            while(c<6){
                data[r][c] = test[c];
                c++;
            }
            r++;
            c = 0 ;
            test = scan.nextLine().split("-");
        }
        System.out.println(data[1][5]);
    }catch(Exception e){
        System.out.println("Error: " + e.getMessage());
    }
    

    然后使用 data[line][index] 访问您的数据。

    【讨论】:

      【解决方案3】:

      我使用以下方法拆分制表符分隔的文件:

      BufferedReader reader = new BufferedReader(new FileReader(path));
      int lineNum = 0; //Use this to skip past a column header, remove if you don't have one
      String readLine;
      while ((readLine = reader.readLine()) != null) { //read until end of stream
          if (lineNum == 0) {
              lineNum++; //increment our line number so we start with our content at line 1.
              continue;
           }
           String[] nextLine = readLine.split("\t");
      
           for (int x = 0; x < nextLine.length; x++) {
               nextLine[x] = nextLine[x].replace("\'", ""); //an example of using the line to do processing.
      
               ...additional file processing logic here...
           }
      }
      

      再次,在我的例子中,我在制表符 (\t) 上进行拆分,但您可以轻松地拆分 - 或任何其他字符,除了换行符。

      根据The Javadoc for readline()A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

      一旦您根据需要拆分线路,只需根据需要将它们分配给您的阵列。

      【讨论】:

        猜你喜欢
        • 2020-04-12
        • 2016-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-22
        • 2022-01-16
        • 1970-01-01
        • 2014-04-06
        相关资源
        最近更新 更多