【发布时间】:2024-06-09 02:50:02
【问题描述】:
现在我有这个错误:
线程“main”中的异常 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)); -
您为什么要以两种不同的方式(Scanner 和 FileReader)读取看似相同的文件两次?
-
@PaulGrime 从技术上讲,他正试图阅读一个并打开另一个:D
-
不是Scanner用于读取(s.hasNextLine() ...)和FileReader用于读取(通过reader.readLine() ...)吗?
-
嗯 也许他想读取一个文件并将其写入另一个文件?他sure会告诉我们:)
标签: java