简体   繁体   中英

How to read content from a file and store it in a 2D array

I am trying to read the contents from a text file and store them in an 2D array, however I have run into an error.

These are the contents of the file:

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 JQ S5 QJ 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 AKA 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 QJQJQ 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 JQAQJ 53 JQAQJ 54 JQJQJ 55 S5 QJQ S5 56 S5 JJJ S5 57 S5 JJJ S5 58 S5 J S1 J S5 59 S5 J S1 J S5 60 A S3 S1 S3 A 61 A S3 S1 S3 A 62 A S3 Q S3 A 63 A S3 Q S3 A 64 AKQKA 65 S1 KQK 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 KQKQK 71 KQKQK 72 KJKJK 73 KJKJK 74 KJKJK 75 S3 JJJ S3 76 S3 NA J NA S3 77 S3 NA J NA S3 78 S3 NA J NA S3

I have tried to do it by splitting each line into a string array, however I have run into the previous error. This is my current code:

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();
   }

}

}

This is the error:

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

EDIT:

After making some changes based on Logan Wlv's remark, I have ran into the following problem. The code is able to read the file successfully, however it does not read the last element of each line. Updated code:

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] + " ";                             
        }
     }

Sample output:

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

The check for sc.hasNextLine() is at wrong place, try following way. Also remove while loop.

    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;
        }
    }

Every-time you have to skip 6 words from the current cursor

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;
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM