Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

arrays - Multiple Choice Java Quiz : How to input questions from a text file?

I'm creating a multiple choice quiz using Java. I have the actual program up and running with all 10 of the questions when they're hard coded into the source code but I need to place 7 of these questions into a text file that will be inputted from a .txt and answered just the same. The only difference of course being those 7 questions that come from a text file instead of being directly in the source code.

Can someone explain or show me a way to get this text file to be inputted into my source code and the quiz up and running this way?

Here is my overall source code:

import java.util.Scanner;
import java.util.*;

    String q1 = "What is hardware?
"
          + "(a)virtual machine
(b)the physical machine
(c)applications such as browsers
(d)part of the processor
";

    String q2 = "What does counter++; do?
"
          + "(a)Adds 1 to counter
(b)Adds 2 to counter
(c)Gets the sum
(d)Multiplies the numbers
";

    String q3 = "What is a loop that never stops?
"
          + "(a)For Loop
(b)Infinite Loop
(c)Do-While Loop
(d)Full Loop
";

    Question [] questions = {
            new Question(q1, "b"),
            new Question(q2, "a"),
            new Question(q3, "b"),
            new Question(q4, "c"),
            new Question(q5, "d"),
            new Question(q6, "a"),
            new Question(q7, "a"),
            new Question(q8, "c"),
            new Question(q9, "a"),
            new Question(qF, "c")
    };

    Collections.shuffle(Arrays.asList(questions));
    takeTest(questions);


}

public static void takeTest(Question [] questions){
    int score = 0;
    Scanner keyboardInput = new Scanner(System.in);


    for(int i = 0; i < questions.length; i++) {
        System.out.println(questions[i].prompt);
        String answer = keyboardInput.nextLine();
        if(answer.equals(questions[i].answer)) {
            score++;
        }
    }
    System.out.println("You got " + score + "/" + questions.length);
}

}

And my text file is simply the seven questions that I had originally placed in the source code :

String q4 = "In a while loop, if the boolean expression is true, what will the loop do?
"
          + "(a)Break
(b)Program will exit
(c)Repeat
(d)Continue through program
";

String q5 = "What special value is designated for controlling a loop?
"
          + "(a)Control value
(b)Mutator Method
(c)Accessor Method
(d)Sentinel Value
";

String q6 = "What is a method?
"
          + "(a)A collection of statements grouped together to perform an operation
(b)A value returned from a method using the return statement
(c)The portion of the program where the variable can be accessed.
(d)The combination of the name of a method and the list of its parameters
";

String q7 = "What is an object?
"
          + "(a)Representation of an entity in the real world that can be distinctly identified
(b)A static method can be called without creating an instance of the class
(c)Instance variable/instance mthod
(d)A template, blueprint or contract that defines what an object's data fields and methods will be.
";

String q8 = "What is an array?
"
          + "(a)Numbers of items ArrayList can store without increasing its size
(b)Number used as an index to pinpoint a specfic element within an array
(c)Object that can store a group of values, all of the same type
(d)Method of locating a specific item in a larger collection of data
";

String q9 = "You use this statement to throw an exception manually.
"
          + "(a)Throw
(b)call stack
(c)try block
(d)thrown
";

String qF = "When an exception is generated, it is said to have been what?
"
          + "(a)Created
(b)Called
(c)Thrown
(d)Generated
";
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use BufferedReader and try to read line by line.

Here an example of using BufferedReader:

BufferedReader br = new BufferedReader(new FileReader("PATH TO QUESTIONS FILE"));
String read = "";
while((read = br.readLine()) != null){
     System.out.println(read);
}

Doing that you will read and print each line of a File.

Make sure you will write each question in a line, otherwise questions will be mixed.

Hope I helped you. :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...