开发者

How to read single question from text file containing several question and options as well as information?

I have a Text file which contains data (4 to 5 paragraph) and then contain 100 question along with each containing four option (a,b,c,d). Later it contain answer for above question (1.a,2.b and so on). My problem is I want to read each time a single question and options to display. Here when the user selects the option I want to check whether he choose the right one or not. Similarly I want to do this 开发者_运维百科for all questions. And at last I want to display how many question are correct and how many are false.

My question is, how can I get each question and option separately? Similarly the answer. How to get information present in text file separately when required?

my file

Section I – General Questions 



Instructions: choose the most correct answer. 

1. The International Labour Organization’s first Maternity Protection Convention was issued in: 

A.) 1952. 
B.) 2000. 
C.) 1919. 
D.) 1997. 

2. Beyond the 5th month of pregnancy, noise in excess of _______________________ may cause 
hearing loss in the fetus: 

A.) 90 dBA TWA per OSHA requirements 
B.) 155 dBC peak and 115 dBC TWA per ACGIH® TLV® Booklet notes 
C.) 75 dBA TWA per American Academy of Pediatrics guidelines 
D.) 65 dBC peak per EPA studies 


ANSWERS – Section I General Questions 

1. C 
2. B 


I wouldn't worry about the format of the file at this point too much. Design your objects (presumably things like Question, Answer, Student etc etc). Get that all working with some test data and then worry about parsing the file. Your program objects don't need to know what the file will look like.


I imagine this text file uses some kind of delimiters to indicate the end of onw questions and the beginning of the next one. If so, I suggest:

  1. Open the text file at the beginning of your program.
  2. When you need to display the first question, read until the first delimiter (the one that occurs between the end of the 1st question and the beginning of the 2nd question).
  3. When you have to show the next question, start from your current position (at the end of question #1), and read until the next delimiter (which would occur after question #2).

The answers can also be read similarly. If you have a special delimiter (say, an XML tag) which indicates the start of the answers, skip ahead up to that position. Then start reading the entries one-by-one, and parsing them.

Sorry for the somewhat vague answer :) If you could be more specific about your requirements, and the delimiters, etc., we can provide more detailed answers.


Use regular expression.

I do not know exact format of your file. I is easier if question can be recognized somehow. For example if the file has format like this:

  1. This is the first question a. aaaaa b. bbbbb c. ccccc 1.b

  2. This is the second question a. aaaaa b. bbbbb c. ccccc 2.c

You can write expression like this: Pattern p = Pattern.compile("^\\d+\\.\\s+(.+?)(?:[abc]\\.\\s+(.+)){3}", Pattern.MULTILINE)

Now just iterate over the pattern:

Matcher m = p.matcher(text);
while (m.find()) {
    String questionNumber = m.group(1);
    String questionText = m.group(2);
    String answerA = m.group(3);
    String answerB = m.group(4);
    String answerC = m.group(5);
    // your code
}

Note that I have not debugged this pattern, so it obviously does not work. But I believe it gives you enough tips, so it is a good start.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜