如何用java将.txt文件读取到2D数组中



我不知道我的程序出了什么问题。我想读取一个名为"的.txt文件;salesReport";但它只读取一行。

2D阵列有10行12列,打印时我还需要帮助将阵列排列为10行12行,中间有3个空格。

我的文本文件

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

我的代码

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

}

这是我的尝试。我在一行的每个元素之间放了三个空格,在列之间放了两个空格。

编辑:更改打印方法以避免使用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("nnn");
}

}
}

输出:

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 

相关内容

  • 没有找到相关文章

最新更新