Maestus Maestus - 15 days ago 5
Java Question

Control a particular line from a text file in java

Hi im wrote a function but theres a problem with that.Function should do if the new string's id same with old strings id the function should return false but when the second string wont allowed whether ids same or not...

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;
}

Answer

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();