Home > Mobile >  How do I read a .txt file into a 2D array in java?
How do I read a .txt file into a 2D array in java?

Time:02-22

I can't figure out what's wrong with my program. I want to read a .txt file named "salesReport" into a 2D array, but it only reads one line.

The 2D array has 10 rows and 12 columns, and I also need help arranging the array, when it is printed, to be in 10 rows and 12 columns with 3 spaces in between.

MY TEXT FILE

31
74
22
05
61
84
14
86
55
22
09
81
83
91
23
49
96
45
41
45
26
77
77
19
84
03
65
91
31
62
73
92
49
93
43
36
69
05
96
54
59
84
14
16
49
35
28
51
60
98
42
61
59
40
01
98
85
80
90
20
22
43
54
71
47
35
81
86
04
61
60
34
35
08
14
44
90
51
27
50
73
53
75
52
19
72
86
41
17
04
46
11
45
25
21
66
96
48
98
06
61
38
11
96
19
63
32
03
11
30
84
80
17
73
90
40
69
55
90
92

MY CODE

import java.util.Scanner;  // Needed for the Scanner class
import java.io.*;  // Needed for the File class

public class salesReport
{
   public static void main(String[] args) throws IOException
   {
     
   // Create a Scanner object to read input.
      Scanner keyboard = new Scanner(System.in);
      
      int[] A;
      int[][] B;
      int i=11,j=13;
      
      A = arrayFile(121);
      
      System.out.println(A);
      
      B = showArray(i,j,A);
     
   }
   
   public static int[] arrayFile(int a) throws IOException
   {
   // Create a Scanner object to read input.
      Scanner keyboard = new Scanner(System.in);
      
      int A[];
      A = new int[a];
   
      System.out.print("Enter the filename: ");
      String filename = keyboard.nextLine();
   
      File file = new File(filename);
      Scanner inputFile = new Scanner(file);
      
      int i=0;
   
      while (inputFile.hasNextLine())
      {
         i  ;
         A[i] = inputFile.nextInt();
      }
      return A;
   }
   
   public static int[][] showArray(int i,int j,int[] C) throws IOException
   {
   // Create a Scanner object to read input.
      Scanner keyboard = new Scanner(System.in);
      
      int[][] A;
      
      A = new int[i][j];
      
      for (int row = 0;row < A.length;row  )
      {
         for (int col = 0;col < A[row].length;col  )
         {
            System.out.print(A[row][col]   " ");
            System.out.println();
         }       
      }
      return A;
   }
    
}

CodePudding user response:

Here is my attempt. I put three spaces between each element in a row and three spaces between the columns.

Edit: Changed print method to avoid using Streams API.

// Needed for the File class
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner; // Needed for the Scanner class

public class Read2D {
    public static void main(String[] args) throws IOException {
        int rows = 10;
        int columns = 12;

        int[] arr = arrayFile(rows * columns);
    
        int[][] md_arr = new int[rows][columns];

        for(int r=0; r < rows; r  ) {
            for(int c=0; c<columns; c  ) {
            md_arr[r][c] = arr[r*c c];
            }
        }
    
        showArray(md_arr);

    }

    public static int[] arrayFile(int a) throws IOException {
        // Create a Scanner object to read input.
        Scanner keyboard = new Scanner(System.in);

        int A[];
        A = new int[a];

        System.out.print("Enter the filename: ");
        String filename = keyboard.nextLine();
        keyboard.close();

        File file = new File(filename);
        Scanner inputFile = new Scanner(file);

        int i = 0;

        while (inputFile.hasNextLine()) {
            A[i] = inputFile.nextInt();
            i  ;
        }
        inputFile.close();

        return A;
    }

    public static void showArray(int[][] C) {
        for(int r=0; r < C.length; r  ) {
            for(int c=0; c<C[r].length; c  ) {
                System.out.print(C[r][c] "   ");
            }
                System.out.print("\n\n\n");
        }
    
    }

}

Output:

31   74   22   5   61   84   14   86   55   22   9   81   


31   22   61   14   55   9   83   23   96   41   26   77   


31   5   14   22   83   49   41   77   84   91   73   93   


31   61   55   83   96   26   84   31   49   69   59   49   


31   84   9   49   26   3   73   36   59   35   42   98   


31   14   83   41   84   73   69   14   60   1   22   81   


31   86   23   77   31   36   14   98   85   71   60   51   


31   55   96   84   49   59   60   85   47   35   73   17   


31   22   41   91   69   35   1   71   35   53   46   6   


31   9   26   73   59   42   22   60   73   46   61   84 
  • Related