我是Java语言的新手。是否有任何方法可以读取文件,直到某个字符再次循环。 示例文本文件将如下所示。
No. Title Score
1 abc 9
Info: adajiodasjdsajkldas adhq sdahdu
dasjkdajkldsa adwq
dasjlkda qe quei qoep
No. Title Score
2 abc 9
No. Title Score
3 abc 9
Info: dasjkdaj aks
dasjlkda
No. Title Score
4 abc 9
Info: dasjkdaj dahjkqi
dasjlkda
我需要的是阅读开头,直到得分和其余部分将被忽略并让它读完后再重新制作一个新循环。 这是我如何编码这个,我不知道如何消除/读取信息,直到新的号码同时信息可以是多行或它可能是空的 这是我编码但未完成的方式,我正在寻求帮助以使其完整。
Scanner read = new Scanner (new File("Student.txt"));
read.useDelimiter("[\\s]+");
String Eliminate[] = new String[99];
String No[] = new String[100];
String Title[] = new String[100];
String Score[] = new String[3];
int i =0;
while (read.hasNext())
{
Eliminate[i] = read.nextLine(); //eliminate first line
No[i] = read.next();
Title[i] = read.next();
Score[i] = read.next();
System.out.println(Eliminate[i] + "\n" + No[i] + " " + Title[i] + " " + Score[i] + "\n" ); // just for debugging
i++;
}
read.close();
答案 0 :(得分:1)
这个怎么样?
while (read.hasNext())
{
if (read.next().equals("No.")){
Eliminate[i] = read.nextLine(); //eliminate first line
No[i] = read.next();
Title[i] = read.next();
Score[i] = read.next();
System.out.println(Eliminate[i] + "\n" + No[i] + " " + Title[i] + " " + Score[i] + "\n" ); // just for debugging
i++;
}
}