简体   繁体   中英

Control a particular line from a text file in java

I have written a function, but it has a problem. The function reads ID strings from a text file and should return true , when it matches an expected ID string value. If no match is found, it should return false .

However, the function always returns false even though I know the text file contains the expected ID. What am I doing wrong?

public boolean WriteOnTxt() throws IOException {
    boolean durum =false;
    FileReader fr = new FileReader("HastaKayit.txt");
    BufferedReader br = new BufferedReader(fr);
    String line;
    String[] parcala;

    while ((line = br.readLine()) != null) {
        line = br.readLine();
        parcala = line.split("#");

        if (parcala[0].equals(this.getPatientID())) {
            durum = true;
            break;
        } else {
            durum = false;  
        }
    }
    br.close();
    return durum;
}

You're skipping lines:

while ((line = br.readLine()) != null) { line = br.readLine();

This section assigns a String to line twice so effectively only running your loop on every other line of data. To fix it, remove the second line = br.readLine();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM