gpt4 book ai didi

java - 用java解析文本文件

转载 作者:行者123 更新时间:2023-11-30 03:57:35 26 4
gpt4 key购买 nike

我有一个 txt 文件,其中包含约 900 个问题,如下所示:

关于问题:

-----------------------------------------------------------------------------
#0001 Which disease devastated livestock across the UK during 2001?
-----------------------------------------------------------------------------
*Hand-and-foot
*Foot-in-mouth
*Hand-to-mouth
*Foot-and-mouth

Answer: Foot-and-mouth

-----------------------------------------------------------------------------
#0002 Which of these kills its victims by constriction?
-----------------------------------------------------------------------------
*Andalucia
*Anaconda
*Andypandy
*Annerobinson

Answer: Anaconda

我有一个存储问题的对象和存储答案的对象

IE:Question.java

public class Question {
private String questionText;
private Answer a, b, c, d;

public Question(String questionText, Answer a, Answer b, Answer c, Answer d) {
this.questionText = questionText;
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}

public String getQuestionText() {
return questionText;
}

public void setQuestionText(String questionText) {
this.questionText = questionText;
}

public Answer getA() {
return a;
}

public void setA(Answer a) {
this.a = a;
}

public Answer getB() {
return b;
}

public void setB(Answer b) {
this.b = b;
}

public Answer getC() {
return c;
}

public void setC(Answer c) {
this.c = c;
}

public Answer getD() {
return d;
}

public void setD(Answer d) {
this.d = d;
}

public String toString() {
return questionText +
"\nA) " + a +
"\nB) " + b +
"\nC) " + c +
"\nD) " + d;
}
}

答案.Java

public class Answer {
private String answerText;
private boolean correct;

//constructor to set correct answer
public Answer(String answerText, boolean correct) {
this.answerText = answerText;
this.correct = correct;
}

public Answer(String answerText) {
this.answerText = answerText;
this.correct = false;
}

public String getAnswerText() {
return answerText;
}

public void setAnswerText(String answerText) {
this.answerText = answerText;
}

public boolean isCorrect() {
return correct;
}

public void setCorrect(boolean correct) {
this.correct = correct;
}

public String toString() {
return answerText;
}
}

我想创建一个数组列表来存储从文本文件解析的所有问题对象。我是 Java 新手,之前主要使用 python 进行编程,并且对如何在 java 中进行文本文件解析感到有点困惑,因为它看起来要复杂得多。例如,我知道如何逐行或单词列表进行解析。我不知道如何处理文件中的额外文本。

如有任何帮助,我们将不胜感激。

两行问题示例:

-----------------------------------------------------------------------------
#0016 Which word follows 'North' and 'South' to give the names of two
continents?
-----------------------------------------------------------------------------
*Africa
*America
*Asia
*Australia

Answer: America

最佳答案

嗨,这里有一些东西可以解决这个问题;)

    String file = "text.txt";
BufferedReader br = null;
int nbAnswer = 4;
try {
br = new BufferedReader(new FileReader(file));
String line;
while((line = br.readLine()) != null) {
if( line.contains("-----------"))
{
line = br.readLine();
String question = line.split("#[0-9]{4} ")[1];
while(!(line = br.readLine()).contains("-----------"))
question += " " + line.trim();

String[] answers = new String[4];

for( int i = 0; i < nbAnswer; i++)
answers[i] = br.readLine().substring(2);

br.readLine();
String sol = br.readLine().split("Answer: ")[1];
System.out.println(question + "\nanswer: " + answers[0] + " " + answers[1] + " " + answers[2] + " " + answers[3] + "\nsol " + sol);
}
}
}
catch(IOException ex) {
System.err.println(ex);
}

line.split("#[0-9]{4} ")[1]; 是一个正则表达式,允许您在 # 后跟 4 个数字和一个空间。

至少这是一个好的开始;)

PS:制作包含问题等的漂亮 .txt 有很多错误。

  1. 解析起来比较困难
  2. 尺寸更大

例如,您可以将 *Foot-and-mouth 更改为 (*)Foot-and-mouth 以表明这就是答案,而不是再添加 2 行为了它;)

关于java - 用java解析文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22764644/

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