【发布时间】:2015-02-15 21:30:01
【问题描述】:
我有一个如下所示的文本文件:
14(*这是后面的数字的行数)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
现在我需要使用 for 循环使用 Scanner 读取这些数据,然后将数字(第一行除外)存储在一个数组中。
到目前为止我有这个,但我卡住了:
import java.io.*;
import java.util.*;
public class DirectoryLookupApplication {
public static void main(String[] args) throws IOException {
Scanner dataFile = new Scanner(new File("Data3.txt"));
int numTimes = dataFile.nextInt(); //since the first line tells us how many numbers are in the file-how many times loop will run
int dataText = new int [numTimes];
}
}
【问题讨论】:
-
文本文件由一个新行分隔 - 所以每个数字都在自己的行上。