現在我有這樣的錯誤:在線程 「主要」 java.lang.ArrayIndexOutOfBoundsException讀取文件從電腦
例外:4 在Lotto1.main(Lotto1.java:37)
37號線:arr [count] [0] = s.next()+「」+ s.next();
import java.io.*;
import java.util.*;
public static void main(String[] args) throws IOException {
File f = new File("D:\\Filipe\\Project Final\\src\\database_lotto.txt");
Scanner s;
try {
s = new Scanner(f);
BufferedReader reader = new BufferedReader(new FileReader(f));
int lines = 0;
while (reader.readLine() != null) {
lines++;
}
reader.close();
arr = new String[lines][3];
int count = 0;
//while theres still another line
while (s.hasNextLine()){
arr[count][0] = s.next() + ""+ s.next();
arr[count][1] = s.next();
arr[count][2] = s.next();
count++;
}
} catch (FileNotFoundException ex) {
System.out.println("File does not exist");
}
將文件放在同一個文件夾,從你在哪裏運行代碼或將文件傳遞給文件讀取器而不是文件的名稱......所以'BufferedReader reader = new BufferedReader(new FileReader(f));' – 2013-04-06 17:46:46
你爲什麼要讀取看起來像相同的文件兩次,並以兩種不同的方式(掃描儀和FileReader)? – 2013-04-06 17:50:14
@PaulGrime從技術上講,他試圖讀取一個並打開另一個:D – 2013-04-06 17:50:54