Jack
Jack

Reputation: 3

Sequential reading from a textfile Javafx

Trying to read sequentially from a textfile. However, currently, only the last line is read when the button is clicked. I want to read all questions from the textfile one after the other. Here's my code:

   buttonNext.setOnAction(new EventHandler<ActionEvent>(){
    public void handle(ActionEvent E){

       java.io.File inFile = new java.io.File("QuestionSheet.txt");


       try {
           java.util.Scanner input = new java.util.Scanner(inFile);

            while (input.hasNext()){
                String[] lineOfCsv = input.nextLine().split(",");
                questionLabel.setText(lineOfCsv[0]);
                aRadio.setText(lineOfCsv[1]);
                bRadio.setText(lineOfCsv[2]);
                cRadio.setText(lineOfCsv[3]);
                dRadio.setText(lineOfCsv[4]);


            }

        } catch (java.io.IOException ex){
        System.out.println("Error reading file " + ex.toString());
        }

       buttonNext.setText("Next Question");

    }
    });

Upvotes: 0

Views: 501

Answers (2)

fabian
fabian

Reputation: 82491

To read each line seperatly keep a reference to the Scanner and call nextLine only once per button click. However this makes it difficult to make sure the Scanner is always properly closed. It would probably be simpler to read the lines to a List<String> and iterating through it instead:

Path filePath = Paths.get("QuestionSheet.txt");
List<String> lines = Files.readAllLines(filePath); // using nio for simplicity here

buttonNext.setOnAction(new EventHandler<ActionEvent>(){

    // iterator for stepwise iterating through the list
    private final Iterator<String> iterator = lines.iterator();

    @Override
    public void handle(ActionEvent event){
        if (iterator.hasNext()) {
            String[] lineOfCsv = iterator.next().split(",");
            questionLabel.setText(lineOfCsv[0]);
            aRadio.setText(lineOfCsv[1]);
            bRadio.setText(lineOfCsv[2]);
            cRadio.setText(lineOfCsv[3]);
            dRadio.setText(lineOfCsv[4]);
            buttonNext.setDisable(!iterator.hasNext());
        }

    }
});

Upvotes: 2

Asad Rehman
Asad Rehman

Reputation: 84

Use buffer reader and input stream to read line by line, The reason is buffer reader contains a default read line function. which helps a lot.

buttonNext.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent E){
try{
FileInputStream fstream = new FileInputStream("QuestionSheet.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null)   {
  System.out.println (strLine);
  String[] lineOfCsv = strLine.split(",");
  questionLabel.setText(lineOfCsv[0]);
  aRadio.setText(lineOfCsv[1]);
  bRadio.setText(lineOfCsv[2]);
  cRadio.setText(lineOfCsv[3]);
  dRadio.setText(lineOfCsv[4]);
}
in.close();
}catch (Exception e){
  System.err.println("Error: " + e.getMessage());
}

Upvotes: 0

Related Questions