现在我有这样的错误:在线程 “主要” 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