public survey loadSurvey(String fileName) throws SecurityException, IOException {
survey loadedSurvey = new survey();
// all the code for reading and parsing .txt file and setting up survey (loading)
System.out.println("What directory is your file in? ");
String whatDir;
BufferedReader readWhatDir = new BufferedReader(new InputStreamReader(System.in));
whatDir = readWhatDir.readLine();
File file = null;
FileReader freader = null;
LineNumberReader lnreader = null;
try{
file = new File(whatDir + fileName + ".txt");
freader = new FileReader(file);
lnreader = new LineNumberReader(freader);
String line = "";
while ((line = lnreader.readLine()) != null) {
if (lnreader.getLineNumber() == 0) {
loadedSurvey.setName(line);
}
else {
if ((line.equals("m/c"))) {
mc newMC = new mc();
String text = lnreader.readLine();
newMC.setQuesText(text);
String lchoices = lnreader.readLine();
while (lchoices.indexOf(".") != -1) {
lchoices = lchoices.substring(0, lchoices.indexOf("."));
newMC.addLeftChoice(lchoices);
}
String rchoices = lnreader.readLine();
while (rchoices.indexOf(".") != -1) {
rchoices = rchoices.substring(0, rchoices.indexOf("."));
newMC.addRightChoice(rchoices);
}
String answer = lnreader.readLine();
newMC.setQuesAnswer(answer);
loadedSurvey.addQues(newMC);
}
else if ((line.equals("t/f"))) {
tf newTF = new tf();
String text = lnreader.readLine();
newTF.setQuesText(text);
// here there is always just T and F but the save function creates two empty lines
// just readLine() twice to get to the user answer
for (int i = 0; i < 2; i++) {
lnreader.readLine();
}
String answer = lnreader.readLine();
newTF.setQuesAnswer(answer);
loadedSurvey.addQues(newTF);
}
else if ((line.equals("matching"))) {
matching newMatching = new matching();
String text = lnreader.readLine();
newMatching.setQuesText(text);
String lchoices = lnreader.readLine();
while (lchoices.indexOf(".") != -1) {
lchoices = lchoices.substring(0, lchoices.indexOf("."));
newMatching.addLeftChoice(lchoices);
}
String rchoices = lnreader.readLine();
while (rchoices.indexOf(".") != -1) {
rchoices = rchoices.substring(0, rchoices.indexOf("."));
newMatching.addRightChoice(rchoices);
}
String answer = lnreader.readLine();
newMatching.setQuesAnswer(answer);
loadedSurvey.addQues(newMatching);
}
else if ((line.equals("ranking"))) {
ranking newRanking = new ranking();
String text = lnreader.readLine();
newRanking.setQuesText(text);
String lchoices = lnreader.readLine();
while (lchoices.indexOf(".") != -1) {
lchoices = lchoices.substring(0, lchoices.indexOf("."));
newRanking.addLeftChoice(lchoices);
}
// right choices are just the numbers to be ranked which is in the constructor
// just perform one readLine() to get to next line
lnreader.readLine();
String answer = lnreader.readLine();
newRanking.setQuesAnswer(answer);
loadedSurvey.addQues(newRanking);
}
else if ((line.equals("essay"))) {
essay newEssay = new essay();
String text = lnreader.readLine();
newEssay.setQuesText(text);
String answer = lnreader.readLine();
newEssay.setQuesAnswer(answer);
loadedSurvey.addQues(newEssay);
}
else if ((line.equals("s/a"))) {
sa newSA = new sa();
String text = lnreader.readLine();
newSA.setQuesText(text);
String answer = lnreader.readLine();
newSA.setQuesAnswer(answer);
loadedSurvey.addQues(newSA);
}
}
}
}
finally {
freader.close();
lnreader.close();
}
return loadedSurvey;
}