0

![enter image description here][1]I have a file called notes.txt containing several lines of text that I want show to my JPanel.

Here is my code:

private void loadNotes() {
    File file = new File("notes.txt");
    if (file.exists()) {
        try {
            FileInputStream fs = new FileInputStream(file);
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(fs));

            for (int i = 0; i < notes.length; i++) {
                if (br.readLine() != null) {
                    String note = br.readLine();
                    System.out.println(note);
                    notes[i] = new JCheckBox(note, false);
                    panel.add(notes[i]);
                    panel.revalidate();
                    panel.repaint();
                }
            }
            br.close();

        } catch (Exception e1) {
        }
    } else {
        System.out.println("File does not exist");
    }
}
            br.close();

This method grabs the lines from the file and prints out the check boxes. So if I have 4 notes then it prints out 4 checkboxes. However, it doesn't print out the text? Why not?

https://i.sstatic.net/SDtMm.png

2
  • Would like to see more of your code. Commented Nov 17, 2013 at 0:00
  • Does the syso print the value of note? Commented Nov 17, 2013 at 0:01

1 Answer 1

4

You're calling br.readLine() twice in the for loop so every second line of the file will be skipped. Assign the note variable at the start of the loop

String note;
for (int i = 0; i < notes.length; i++) {
    if ((note = br.readLine()) != null) {
        ...
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

i.sstatic.net/YVX8Y.png My result. I removed the if (bt.readLine() != null) correct? I just want each line to print out.... -.-
What does the input file look like?
No spaces, no extra boxes. So aggravating Input file: i.sstatic.net/SDtMm.png
Couldnt have known that, kinda important to post an SSCCE to help us diagnose such issues :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.