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

后端 未结 3 1645
花落未央
花落未央 2020-11-29 13:38

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

相关标签:
3条回答
  • 2020-11-29 14:02

    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.

    0 讨论(0)
  • 2020-11-29 14:06

    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;
    
    0 讨论(0)
  • 2020-11-29 14:24

    You are reading the line twice..

    while ((read = in.readLine()) != null) { // here
                read = in.readLine();      // and here
    
    0 讨论(0)
提交回复
热议问题