java - do while loop for text file not functioning -
i need read in text file unknown number of questions. formatting same, number of options not. have taken care of loop.
if remove while, code fetches 1 of questions , stores it, need loop. code won't compile loop. have tried many fixes, won't compile.
any advice appreciated!
filereader filereader = new filereader(f); bufferedreader bufferedreader = new bufferedreader(filereader); string line; { line = bufferedreader.readline(); string question = line; line = bufferedreader.readline(); int numoptions = integer.parseint(line); arraylist<string> options = new arraylist<>(); (int = 0; < numoptions; i++){ line = bufferedreader.readline(); string choice = line; options.add(choice); } line = bufferedreader.readline(); int answer = integer.parseint(line); line = bufferedreader.readline(); int tries = integer.parseint(line); line = bufferedreader.readline(); int wins = integer.parseint(line); question objq = new question(question, numoptions, options, answer, tries, wins); mainlist.add(objq); } while (bufferedreader.readline() != null); bufferedreader.close();
in while statement, bufferedreader.readline() don't use result. maybe not intented do.
please have @ code below, should compile , read questions text file (assuming 1 integer per line).
import java.io.bufferedreader; import java.io.file; import java.io.filereader; import java.io.ioexception; import java.util.arraylist; public class readquestions { static arraylist<question> mainlist = new arraylist<>(); static void readfile(file f) throws ioexception { filereader filereader = new filereader(f); bufferedreader bufferedreader = new bufferedreader(filereader); string line; while(true) { line = bufferedreader.readline(); if(line == null) break; system.out.println(line); string question = line; line = bufferedreader.readline(); int numoptions = integer.parseint(line); arraylist<string> options = new arraylist<>(); (int = 0; < numoptions; i++) { line = bufferedreader.readline(); string choice = line; options.add(choice); } line = bufferedreader.readline(); int answer = integer.parseint(line); line = bufferedreader.readline(); int tries = integer.parseint(line); line = bufferedreader.readline(); int wins = integer.parseint(line); question objq = new question(question, numoptions, options, answer, tries, wins); mainlist.add(objq); } bufferedreader.close(); } public static void main(string []arg) throws ioexception { file f = new file("questions.txt"); readfile(f); } public static class question { public question(string question, int numoptions, arraylist<string> options, int answer, int tries, int wins) { // tbc } } }
Comments
Post a Comment