So anyway I have a class called QuestionWriter, which I add to the world under certain conditions; this class happens to contain the code that displays actual questions on the screen. Once the player has submitted an answer, I remove the QuestionWriter class., and the questions along with it. These questions are loaded from a .csv file and stored in a 2d array in the QuestionWriter class, using the algorithm below:
The problem is trying to display the next question after the first question has been removed from the world. I always seem to get the same question appearing, though the code should go to the next row of the array, thus displaying a new question. Note how instance increases, but still no row change:
Can anyone tell me what I've done wrong, because to my understanding if Level.instance increases, the row of the array should increase to, changing the question that is displayed. Thanks again for all feedback!
FileReader questionFile = new FileReader("Questions Log.csv"); BufferedReader readQuestions = new BufferedReader(questionFile); for (count = 0; count < maxQuestions; count++) { // read in the file, one line at a time lineNo = readQuestions.readLine(); // reads a Line into 'line' if (lineNo == null) { break; // if no lines left exit for loop } for (field = 0; field < 5; field++) // break the line down, one field at a time { if (lineNo.indexOf(',') < 0) { break; // if no seperators left, exit for loop (only one field left) } data[count][field] = lineNo.substring(0, lineNo.indexOf(',')); // save first field of line lineNo = lineNo.substring(lineNo.indexOf(',') + 1); // remove first field from line } data[count][field] = lineNo; // save last field of line } questionCount = count; // save total questions read in... sort(data); //don't worry about this part- it does its job! }
//in the World public void displayQuestions () { try { addObject(questionWriter, 241, 267); questionWriter.generateQuestion(); instance++; //swop to next question for next time... } catch (java.io.IOException h){ System.out.println ("Failed to Load Questions :("); } } //in QuestionWriter.class public void generateQuestion () throws IOException { //the code below assigns each part of the array containing the questions to a variable. question = data[Level.instance][0]; //originally set to zero. optionA = data[Level.instance][1]; optionB = data[Level.instance][2]; optionC = data[Level.instance][3]; optionD = data[Level.instance][4]; actualAnswer = data[Level.instance][5]; //the code below adds the classes that appear as the the question and options to the user. getWorld().addObject(opt1, 169, 251); getWorld().addObject(opt2, 342,251); getWorld().addObject(opt3, 169, 320); getWorld().addObject(opt4, 342, 320); getWorld().addObject(mathsQuestion, 236, 173); }