我有一个带有多项选择问题和答案的TXT文件(如150题),这是格式:
我需要在文件中查找并获取问题和答案,以便在UI界面中显示它们。
目前,我可以阅读并打印文件,但我不知道如何分开判决。 有什么建议吗?
代码:
import java.io.*;
import java.nio.file.Path;
public class fileManager {
public FileInputStream inputStream;
public InputStreamReader reader;
public File myfile;
public String question;
public String [] answer;
public fileManager(String myfile) {
this.myfile = new File(String.valueOf(myfile));
try {
inputStream = new FileInputStream(this.myfile );
try {
reader = new InputStreamReader(inputStream , "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void printFile(){
int indexChar = 1;
char concatination = '.';
int endFile = 0;
try {
endFile = inputStream.available();
} catch (IOException e) {
e.printStackTrace();
}
do {
try {
char mychar = (char)reader.read();
if (mychar == ((char)indexChar)){
if(concatination == (char)reader.read()){
do{
System.out.print((char)reader.read());
}while ((char)reader.read() == 'א');
}
}
endFile++;
} catch (IOException e) {
e.printStackTrace();
}
}while(endFile < 1000);
}
public void closeFile(){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
} //End function
public void getChar(){
}
public void getTheQuestion(){
int questionNum = 0;
int eof = 0;
int i =0;
String []file;
String question;
try {
eof = inputStream.available();
} catch (IOException e) {
e.printStackTrace();
}
for (;i == '1';){
try {
i = reader.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}//end getTheQuestion
//Getters and Setters
public FileInputStream getInputStream() {
return inputStream;
}
public void setInputStream(FileInputStream inputStream) {
this.inputStream = inputStream;
}
public File getMyfile() {
return myfile;
}
public void setMyfile(File myfile) {
this.myfile = myfile;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String[] getAnswer() {
return answer;
}
public void setAnswer(String[] answer) {
this.answer = answer;
}
}
答案 0 :(得分:2)
您可以使用正则表达式完成它。我在这里写了一个帮助的程序
我已经为问题和所有四个选项创建了Pattern
,然后获取了它们。
File file = new File("myfile.txt");
Scanner sc = new Scanner(file);
Pattern questionPattern = Pattern.compile("(^(.+\\?)(?=(\\s+(a\\.\\s+.+)"
+ "\\s+b\\.\\s+.+\\s+c\\.\\s+.+\\s+d\\.\\s+.+)))", Pattern.CASE_INSENSITIVE);
Pattern optionAPattern = Pattern.compile("((?<=(.+\\?\\s))(a\\..+)(?=(\\sb\\..+$)))");
Pattern optionBPattern = Pattern.compile("((?<=(\\s))(b\\..+)(?=(\\sc\\..+$)))");
Pattern optionCPattern = Pattern.compile("((?<=(\\s))(c\\..+)(?=(\\sd\\..+$)))");
Pattern optionDPattern = Pattern.compile("((?<=(\\s))(d\\..+)(?=(\\s*$)))");
if (sc.hasNextLine()) {
String line = sc.nextLine();
Matcher question = questionPattern.matcher(line);
Matcher optionA = optionAPattern.matcher(line);
Matcher optionB = optionBPattern.matcher(line);
Matcher optionC = optionCPattern.matcher(line);
Matcher optionD = optionDPattern.matcher(line);
if(question.find()) System.out.println(question.group());
if(optionA.find()) System.out.println(optionA.group());
if(optionB.find()) System.out.println(optionB.group());
if(optionC.find()) System.out.println(optionC.group());
if(optionD.find()) System.out.println(optionD.group());
}
输出
what's your name?
a. danny
b. pedro
c. jose
d. mikey
我认为你是初学者。尝试学习正则表达式以理解代码
答案 1 :(得分:1)
我使用afzalex解决方案, 扫描器看线,所以我想我的行以“索引字母”开头(实际上是希伯来语)所以我理解它的答案,你可以看看createPatterns(),问题不是答案所以我离开了他们,我决定这将在IF句子的末尾,如果不是空的,请输入添加问题。
这里是代码:
import com.sun.org.apache.xerces.internal.impl.xpath.regex.Match;
import java.io.*;
import java.nio.file.Path;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by aby on 11/9/14.
*/
public class fileManager {
public File myfile;
public Pattern myAnswerAlef;
public Pattern myAnswerBet;
public Pattern myAnswerGimel;
public Pattern myAnswerDalet;
public Scanner myscanner;
List<String> question = new ArrayList<String>();
List<String> answerAlef = new ArrayList<String>();
List<String> answerBet = new ArrayList<String>();
List<String> answerGimel = new ArrayList<String>();
List<String> answerDalet = new ArrayList<String>();
public fileManager(String myfile) {
this.myfile = new File(String.valueOf(myfile));
try {
myscanner = new Scanner(this.myfile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
getAnswerQuestions();
}
public void closeFile(){
myscanner.close();
} //End function
public void createPatterns(){
myAnswerAlef = Pattern.compile("[א]+[.]");
myAnswerBet = Pattern.compile("[ב]+[.]");
myAnswerGimel = Pattern.compile("[ג]+[.]");
myAnswerDalet = Pattern.compile("[ד]+[.]");
}
public void getAnswerQuestions(){
createPatterns();
do {
String line = myscanner.nextLine();
if(line.length() != 1){
Matcher MatcherAnswerAlef = myAnswerAlef.matcher(line);
Matcher MatcherAnswerBet = myAnswerBet.matcher(line);
Matcher MatcherAnswerGimel = myAnswerGimel.matcher(line);
Matcher MatcherAnswerDalet = myAnswerDalet.matcher(line);
if (MatcherAnswerAlef.find()){
answerAlef.add(line);
}
else
if (MatcherAnswerBet.find()){
answerBet.add(line);
}
else
if (MatcherAnswerGimel.find()){
answerGimel.add(line);
}
else
if (MatcherAnswerDalet.find()){
answerDalet.add(line);
}
else
if (!line.isEmpty()){
question.add(line);
}
}
else{
continue;
}
}while (myscanner.hasNext());
}
}