我是编程新手,在获取带有数字列表的文件并将其转换为整数数组时遇到了一些麻烦,然后我可以在格式化的列(5 行和 10 列)中打印。我想我使用 an 进行了正确的导入,ArrayList
但是当我尝试打印列时遇到了问题。我想我需要使用 for 循环来打印列,但我不是 100% 确定。任何数量的帮助将不胜感激!这是我的代码:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Numbers {
private List < Integer > data = new ArrayList < Integer > (); //Create ArrayList
public static void main(String[] args) {
String filename = "C/Users/s/Desktop/file4/Input.txt";
Numbers rfe = new Numbers();
rfe.readFile(filename);
}
private void readFile(String name) {
String input;
try (BufferedReader reader = new BufferedReader(new FileReader(name))) {
while ((input = reader.readLine()) != null) {
data.add(Integer.parseInt(input)); //Add each parsed number to the arraylist
int[] i = input;
for (i; i < null; i++) {
System.out.format("%20s, %10s", i);
}
}
} catch (FileNotFoundException fnfe) {
} catch (IOException ioe) {
} catch (Exception ex) { // Not required, but a good practice
ex.printStackTrace(); //Usually good for general handling
}
}
}
这是文件包含的内容(一行中的每个数字):
32
73
63
47
72
34
26
84
27
75
95
10
48
88
28
65
71
40
14
11
67
76
77
80
12
15
30
74
13
41
21
22
57
17
99
92
52
38
18
46
62
64
39
16
43
29
79
49
19
60