-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuizGameLoader.java
More file actions
104 lines (100 loc) · 3.89 KB
/
Copy pathQuizGameLoader.java
File metadata and controls
104 lines (100 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class QuizGameLoader {
// Arrays and ArrayList to store the questions, answers, and options
ArrayList<String> questions = new ArrayList<String>();
ArrayList<String> answers = new ArrayList<String>();
String[][] options = new String[90][4];
public void loadQuiz(String filePath){
try {
// Acess the txt file
File file = new File(filePath);
Scanner scanner = new Scanner(file);
int questionCount = 0;
//Scann until there nothing is left to scan
while(scanner.hasNextLine()){
String line = scanner.nextLine();
// Once "Correct Answers" is encountered by the scanner, it indicates that the next lines contain the answers to the questions
// so we must then start storing the answers now
if(line.equals("Correct Answers:")) {
while(scanner.hasNextLine()){
line = scanner.nextLine();
answers.add(line.trim());
}
break;
}
// Add the question
questions.add(line);
// Since every question has four answer choices run a for loop
// to read and store the next four lines in the options array
for(int i = 0; i < 4; i++){
line = scanner.nextLine();
options[questionCount][i] = line.trim();
}
line = scanner.nextLine();
questionCount++;
}
scanner.close();
}
catch (FileNotFoundException e) {
throw new RuntimeException("File not found: " + filePath);
}
}
//Generate a random list of questions from the questions bank
public boolean isNewNUm(int num, int[] randQuestions){
Set<Integer> uniqueIndices = new HashSet<>();
for(int i =0; i < randQuestions.length; i++){
if(randQuestions[i] == num){
return false;
}
}
return true;
}
//Check users answer choices
public void checkWork(String[] answers, int[] questions, int numQuesitons){
int correct = 0;
int incorrect = 0;
ArrayList<Integer> incorrectIdx = new ArrayList<Integer>();
for(int i = 0; i < numQuesitons; i++){
String[] lines = this.answers.get(questions[i]).split("\\)");
String answerChoice = lines[0].trim();
String wordChoice = lines[1].trim();
if(answers[i].equalsIgnoreCase(answerChoice)){
correct++;
}
else if(answers[i].equalsIgnoreCase(wordChoice)){
correct++;
}
else{
incorrectIdx.add(questions[i]);
incorrect++;
}
}
System.out.println("You scored: " + correct + "/" + numQuesitons);
if(correct < numQuesitons){
System.out.println("Incorrect Choices:");
for(int i = 0; i < incorrect; i++){
String line = this.questions.get(incorrectIdx.get(i));
System.out.println(line);
for(int j= 0; j < 4; j ++){
String option = options[incorrectIdx.get(i)][j];
System.out.println(option);
}
System.out.println("You choose: " + answers[i]);
System.out.println("Correct Answer: " + this.answers.get(incorrectIdx.get(i)));
System.out.println();
}
}
}
// Getters for the Questions, options, and answers.
public List<String> getQuestions() {
return questions;
}
public String[][] getOptions() {
return options;
}
public List<String> getAnswers() {
return answers;
}
}