【发布时间】:2015-02-01 20:20:29
【问题描述】:
我有一个名为“animals.txt”的输入文件:
sheep 10.5 12.3 4
horse 8.4 11.2 7
cow 13.7 7.2 10
duck 23.2 2.5 23
pig 12.4 4.6 12
简单地说,我想知道如何将输入文件中的 4 列数据存储到 4 个单独的一维数组中。
输出应该是这样的......
[sheep, horse, cow, duck, pig]
[10.5, 8.4, 13.7, 23.2, 12.4]
[12.3, 11.2, 7.2, 2.5, 4.6]
[4, 7, 10, 23, 12]
到目前为止,我已经弄清楚如何将所有数据存储到一个大数组中,但我需要知道如何将其分解并将每一列存储到自己的数组中。
我的代码:
public static void main(String[] args) throws FileNotFoundException {
String[] animal = new String[5];
int index = 0;
File file = new File("animals.txt");
Scanner input = new Scanner(file);
while (input.hasNextLine() && index < animal.length) {
animal[index] = input.nextLine();
index++;
}
【问题讨论】: