【发布时间】:2013-05-13 13:33:07
【问题描述】:
我必须用文本文件中的整数填充一个数组,我需要文件阅读器从每一行中获取一个整数并放入一个数组中,但它也不能将重复项放入数组中,这使得它甚至更复杂,而且重复,我必须将它们写入另一个文本文件,例如:sorted.txt,我无法弄清楚如何在我大学的第一年做所有这些。如果有人可以提供帮助将不胜感激。提前谢谢你
这是我迄今为止在我的方法中得到的结果
public static void readFromfile()throws IOException {
List<String> lines = new ArrayList<String>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("file.txt"));
String line = null;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
} finally {
reader.close();
}
int[] array = lines.toArray();// i keep getting incopatible type error in this line
awell
在过去的 6 天里,我一直在做这件事,这就是我取得的成就:(
【问题讨论】:
-
文本文件中的每一行包含 1 个 int?
-
第一个错误是您尝试将字符串列表转换为 int 数组:int[] array = lines.toArray();
标签: java arrays filereader