gpt4 book ai didi

Java:有没有办法读取文件直到某个字符

转载 作者:行者123 更新时间:2023-11-30 07:44:45 27 4
gpt4 key购买 nike

我是 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();

最佳答案

这个怎么样?

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++;
}
}

关于Java:有没有办法读取文件直到某个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34084972/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com