[英]Scanner for input file and storing data objects from input file in array
基本上,我必須為給定文件創建一個掃描程序,並在計算文件中的行數后讀取文件(該名稱由用戶通過終端輸入)。 之后,我不得不從文件中創建一個具有正確大小的對象數組(其中有數量的行進入)。 然后我不得不為該文件創建另一個掃描程序並再次讀取它,將其存儲在我創建的數組中。 最后,必須在方法中返回數組。
我的問題是我似乎無法讓第二個掃描程序實際存儲數組中的文件對象。
我嘗試在也調用數組的for循環中使用.nextLine
,但它似乎不起作用。
public static Data[] getData(String filename) {
Scanner input = new Scanner(new File(filename));
int count = 0;
while (input.hasNextLine()) {
input.nextLine();
count++;
}
System.out.println(count);
Data[] data = new Data[count];
Scanner input1 = new Scanner(new File(filename));
while (input1.hasNextLine()) {
for (int i = 0; i < count; i++) {
System.out.println(data[i].nextLine);
}
}
return data;
}
我希望輸出成功讀取輸入文件,以便可以通過我創建的其他方法(未顯示)訪問它。
不清楚Data.class是什么意思,如果你把它切換到String,問題顯然就在這一行
System.out.println(data[i].nextLine);
如果你想分配和打印同時寫這個
for (int i = 0; i < count; i++) {
data[i] = input1.next();
System.out.println(data[i]);
}
並且不要忘記關閉掃描儀,更好地使用try-with-resources
。 如果您的數據是您的自定義類,您最好了解Serialization-Deserialization
或使用一些ObjectMapper-s(例如Jackson)來存儲您的類實例並恢復它們。
打開文件只是為了對行進行計數然后再循環遍歷它們以將它們存儲在數組中的方式效率不高,但它可能只是一個學校作業。
嘗試這個:
public static Data[] getData(String filename) {
Scanner input = null;
try {
input = new Scanner(new File(filename));
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
int count = 0;
while (input.hasNextLine()) {
input.nextLine();
count++;
}
input.close();
System.out.println(count);
Data[] data = new Data[count];
try {
input = new Scanner(new File(filename));
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
for (int i = 0; i < count; i++) {
Data d = new Data(input.nextLine(), 0, 0);
data[i] = d;
System.out.println(data[i].name);
}
input.close();
return data;
}
在第一個循環之后,您必須關閉Scanner
並重新打開它,以便從文件的第一行開始全部。
如果你沒有IDE,你肯定應該使用IDE,嘗試intellij ...你有自動完成和語法檢查等等。
目前還不清楚你想要在for循環中做什么,因為有幾個錯誤,例如readline()
函數只能用於scan objekt,所以你可以做input.nextline()
或input1.nextline()` ...
所以我只是告訴你,如何使用Scanner從文件中獲取數據:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Readfile {
public static void getData(String filename) throws FileNotFoundException {
ArrayList<String> test = new ArrayList<>(); //arraylist to store the data
Scanner inputSc = new Scanner(new File(filename)); //scanner of the file
while (inputSc.hasNextLine()) {
String str = inputSc.nextLine();
System.out.println(str); //print the line which was read from the file
test.add(str); //adds the line to the arraylist
//for you it would be something like data[i] = str; and i is a counter
}
inputSc.close();
}
public static void main(String[] args) {
try {
getData("/home/user/documents/bla.txt"); //path to file
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
您不需要通過文件讀取兩次 - 只需使用ArrayList來保存從文件中傳入的數據,如下所示,然后在結尾處返回Data []:
public static Data[] getData(String filename) {
List<Data> result = new ArrayList<>();
try (Scanner input = new Scanner(new File(filename))){
while (input.hasNextLine()) {
Data data = new Data(input.nextLine());
result.add(data);
}
}
return result.toArray(new Data[0]);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.