Hi there here is something that might do the trick ;)
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];
is a regex that will allow you to split the string after a # followed by 4 numbers and a space.
At least it's a good beginning ;)
PS: There are many wrong thing about doing a beautiful .txt containing the questions etc.
- It's harder to parse
- It's bigger in size
You could for example change *Foot-and-mouth
to (*)Foot-and-mouth
to indicate that this is the answer instead of having 2 more lines for it ;)