如何从文件中读取内容并将其存储在2D数组中

时间:2019-05-16 12:52:34

标签: java arrays

我正在尝试从文本文件中读取内容并将其存储在2D数组中,但是我遇到了错误。

这些是文件的内容:

R1 R2 R3 R4 R5

1 J S2 Q S2 J 2 J S2 Q S2 J 3 J S2 Q S2 J 4 J S2 Q S2 J 5 J Q S5 Q J 6 S3 Q S5 Q S3 7 S3 Q S5 Q S3 8 S3 Q S5 Q S3 9 S3 S1 S5 S1 S3 10 A S1 S5 S1 A 11 A S1 K S1 A 12 A S1 K S1 A 13 A S1 K S1 A 14 A S1 K S1 A 15 S2 A K A S2 16 S2 A S3 A S2 17 S2 A S3 A S2 18 S2 A S3 A S2 19 S2 A S3 A S2 20 Q S5 S3 S5 Q 21 Q S5 J S5 Q 22 Q S5 J S5 Q 23 Q S5 J S5 Q 24 Q S5 J S5 Q 25 S4 K S2 K S4 26 S4 K S2 K S4 27 S4 K S2 K S4 28 S4 K S2 K S4 29 S4 K S2 K S4 30 K S4 S2 S4 K 31 K S4 A S4 K 32 K S4 A S4 K 33 K S4 A S4 K 34 K S4 A S4 K 35 S1 S4 A S4 S1 36 S1 A S1 A S1 37 S1 A S1 A S1 38 S1 A S1 A S1 39 S1 A S1 A S1 40 Q S3 S1 S3 Q 41 Q S3 Q S3 Q 42 Q S3 Q S3 Q 43 Q S3 Q S3 Q 44 Q J Q J Q 45 S2 J S4 J S2 46 S2 J S4 J S2 47 S2 J S4 J S2 48 S2 S2 S4 S2 S2 49 S2 S2 A S2 S2 50 J S2 A S2 J 51 J S2 A S2 J 52 J Q A Q J 53 J Q A Q J 54 J Q J Q J 55 S5 Q J Q S5 56 S5 J J J S5 57 S5 J J J S5 58 S5 J S1 J S5 59 S5 J S1 J S5 60安S3 S1 S3 A 61 A S3 S1 S3 A 62安S3 Q S3 A 63 A S3 Q S3 A 64 A K Q K A 65 S1 K Q K S1 66 S1 K S4 K S1 67 S1 K S4 K S1 68 S1 Q S4 Q S1 69 S1 Q S4 Q S1 70 K Q K Q K 71 K Q K Q K 72 K J K J K 73 K J K J K 74 K J K J K 75 S3 J J J S3 76 S3不适用J不适用S3 77 S3不适用J不适用S3 78 S3 NA J NA S3

我试图通过将每一行拆分为一个字符串数组来实现此目的,但是我遇到了上一个错误。 这是我当前的代码:

package slots;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

public class Slots {

public static void main(String[] args) throws FileNotFoundException 
{
   Scanner sc = new Scanner(new BufferedReader(new FileReader("reels_template.txt")));

   String [][] arr = new String[78][6];

   while(sc.hasNextLine()) {
     for (int i=0; i<78; i++) {
         String[] line = sc.nextLine().trim().split(" ");
        for (int j=0; j<5; j++) {
            arr[i][j] = line[j];
        }
     }
  }

   for (int i = 0; i < 78; i++) {
       for ( int j = 0; j < 6; j++) {
           System.out.print(arr[i][j]);
       }
       System.out.println();
   }

}

}

这是错误:

Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1585)
    at slots.Slots.main(Slots.java:19)
Java Result: 1

编辑:

根据洛根·沃尔夫(Logan Wlv)的言论进行了一些更改之后,我遇到了以下问题。该代码能够成功读取文件,但是不会读取每行的最后一个元素。更新的代码:

Scanner sc = new Scanner(new BufferedReader(new FileReader("reels_template.txt")));

   String [][] arr = new String[78][6];
   String[] line = {};


     for (int i=0; i<78; i++) {
         if (sc.hasNextLine()) {
         line = sc.nextLine().trim().split("\\s+");
         }
        for (int j=0; j<5; j++) {        
            arr[i][j] = line[j] + " ";                             
        }
     }

示例输出:

# R1 R2 R3 R4 null
1 J S2 Q S2 null
2 J S2 Q S2 null
3 J S2 Q S2 null

2 个答案:

答案 0 :(得分:0)

检查sc.hasNextLine()的位置不正确,请尝试按照以下方式进行。同时删除while循环。

    for (int i=0; i<78; i++) {
        if(sc.hasNextLine()) {
            String[] line = sc.nextLine().trim().split(" ");
            for (int j=0; j<5; j++) {
                arr[i][j] = line[j];
            }
        } else {
            break;
        }
    }

答案 1 :(得分:0)

每次您都必须从当前光标跳过6个单词

while (sc.hasNextLine()) {
            String line = sc.nextLine();
            String[] words = line.trim().split(" ");
            int k = 0;
            for (int i = 0; i < 78; i++) {
                for (int j = 0; j < 6; j++) {
                    arr[i][j] = words[i + j + k];

                }
                k += 5;
            }
        }