I have a TXT file with a multiple choice question and answer(like 150 question),this is the format:
I need to seek in the file and get the questions and the answer to show them in a UI interface.
For the moment, I can read and print the file, but I don't know how to get the sentence for separate. Any suggestion?
The code:
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;
}
}
You can accomplish it with regular expressions. Here I have written a program to help.
I have created Pattern
's for questions and all four options and then fetched them.
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());
}
Output :
what's your name?
a. danny
b. pedro
c. jose
d. mikey
I think you are a beginner. Try learning regular expression to understand the code
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments