BufferedReader is skipping every other line when reading my file in java
Asked Answered
B

4

5

So Im working of reading a file containing appointments that I wrote to earlier in my code. I want to sift through the text file and find appointments on a certain date and add them to an ArrayList but when the BufferedReader goes through it, it skips ever other line... Heres my code

public ArrayList<String> read(int checkDay, int checkMonth, int checkYear) {
    ArrayList<String> events = new ArrayList<String>();
    BufferedReader in = null;
    String read;
    try {
        in = new BufferedReader(new FileReader("calendar.txt"));
        while ((read = in.readLine()) != null) {
            read = in.readLine();

            String[] split = read.split(",");
            System.out.println(read);

            if (split[1].equals(Integer.toString(checkDay)) && split[2].equals(Integer.toString(checkMonth)) && split[3].equals(Integer.toString(checkYear))) {
                events.add(split[0] + " : " + split[1] + "/" + split[2] + "/" + split[3]);
            }

        }
    } catch (IOException e) {
        System.out.println("There was a problem: " + e);
        e.printStackTrace();

    } finally {
        try {
            in.close();
        } catch (Exception e) {
        }

    }
    return events;
}
Benoni answered 6/10, 2014 at 9:56 Comment(0)
C
10

You are reading the line twice..

while ((read = in.readLine()) != null) { // here
            read = in.readLine();      // and here
Claustrophobia answered 6/10, 2014 at 9:57 Comment(0)
M
3

You have error here:

while ((read = in.readLine()) != null) 
 read = in.readLine();

you should keep the read = in.readLine() in the while. and remove the other line.

Modular answered 6/10, 2014 at 10:1 Comment(0)
I
1

pl try this

you r using "read = in.readLine())" two times in while loop that why it is skiping the lomes

public ArrayList<String> read(int checkDay, int checkMonth, int checkYear) {
        ArrayList<String> events = new ArrayList<String>();
        BufferedReader in = null;
        String read;
        try {
            in = new BufferedReader(new FileReader("calendar.txt"));
            while ((read = in.readLine()) != null) {

                String[] split = read.split(",");
                System.out.println(read);

                if (split[0].equals(Integer.toString(checkDay)) && split[1].equals(Integer.toString(checkMonth)) && split[2].equals(Integer.toString(checkYear))) {
                    events.add(split[0] + " : " + split[1] + "/" + split[2] + "/" + split[3]);
                }

            }
        } catch (IOException e) {
            System.out.println("There was a problem: " + e);
            e.printStackTrace();

        } finally {
            try {
                in.close();
            } catch (Exception e) {
            }

        }
        return events;
Indium answered 6/10, 2014 at 10:10 Comment(1)
Where is the answer here?Claustrophobia
S
0

may be usage of "next()" over "nextLine()" should resolve the issue:

System.out.println("Enter Emp ID: ");
        eid = scanner.nextInt();

        System.out.println("Enter Emp Name:");
        ename = scanner.next();

        System.out.println("Enter Emp Salary:");
        esal = scanner.nextFloat();

        System.out.println("Enter Emp Address:");
        eaddr = scanner.next();
Slide answered 27/3, 2023 at 12:54 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.