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

此生再无相见时 提交于 2019-11-27 16:17:12

You are reading the line twice..

while ((read = in.readLine()) != null) { // here
            read = in.readLine();      // and here

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.

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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!