I'm building a CLI survey app in Java that lets users answer multiple-choice questions. Please tell me how my approach can be improved.
Each answer to a question is recorded as an int, which is the index of that choice in the array of choices for that question. All answers for a survey are saved as an int array. I'm thinking of serializing this int array using ObjectOutputStream. Then when I need to display the results, I'll restore the array and print each element after a tab.
The problem with this approach is (I believe) the second set of responses overwrites the first. I thought of two alternatives, both of which sound bad as well. One is saving each response set to a separate file. But then come display time, I'll have to read all the files at once to keep responses for the same question together. The other is saving the int array as a tab-separated line in a plain text file (so each response set makes a new line), then tokenize and parse it back to an int array for displaying. But then the tokenizing/parsing code is a horror to read (currently it looks like this):
EDIT: Whoops, this code isn't even right. My point is just that parsing is a mess, so I'm looking for a way not to have to parse.
File savedResults = new File(Main.surveyResultsFolder, surveyFileName);
try {
BufferedReader br = new BufferedReader(new FileReader(savedResults));
int[] currentResponseSet = new int[noOfQuestions];
String currentResponseString = "";
String[] currentResponseStrArray = null;
while((currentResponseString = br.readLine()) != null) {
currentResponseStrArray = currentResponseString.split("\t");
for (int i = 0; i < currentResponseStrArray.length; i++) {
currentResponseSet[i] = Integer.parseInt(currentResponseStrArray[i]);
}
}
//then I'll print currentResponseSet here.
}
catch (IOException ex) {
System.out.println("Error reading results file " + surveyFileName);
}
I'm out of ideas. As you can see, my knowledge of data handling techniques is limited. Any takers?