I am trying to find duplicates values from the first column 0 in array that's been loaded in a string and split, and then throw an exception when this is found.
Here is my code.
public void loadTrueFalseQuestions() throws Exception {
try {
Scanner input = new Scanner(new FileInputStream(TRUE_FALSE_FILE));
while (input.hasNextLine()) {
line = input.nextLine();
String[] split = line.split(",");
int chapterNumber = Integer.parseInt(split[1]);
String correctAnswer = (split[3]);
String questionID = (split[0]);
if (split.length != TRUE_FALSE_FIELDS) {
throw new Exception();
} else if ((!correctAnswer.matches("TRUE")) & (!correctAnswer.matches("FALSE"))) { //throw new Exception();
} else if (true) {
}
}
} catch (FileNotFoundException e) {
System.out.println("Could not open a file.");
System.exit(0);
}
}
This is the CSV file.
TF001,8,Java allows an instance of an abstract class to be instantiated.,FALSE
TF001,8,Downcasting should be used only in situations where it makes sense.,TRUE
TF003,9,The throw operator causes a change in the flow of control.,TRUE
TF004,9,When an exception is thrown the code in the surrounding try block continues executing
and then the catch block begins execution.,FALSE
I'm not sure how to go about it. I cannot get past the logic. I'm trying an if statement and contains(questionID) string method, but not sure how to combine these two. (if that makes sense).
Appreciate any advice.
splitafter accessingsplit's members. You need to do that immediately after callingline.split.