java - infinite loop issue in my code : my programm seems not stopping -
/* have file containing data: san francisco: 19887.32
chicago: no report received
new york: 298734.12
los angelos: no report received
, want print cities
code seems it's not stopping!*/
public static void main(string[] args) { try (bufferedreader br = new bufferedreader(new filereader("sales.dat"))) { string line; char c; while ((line = br.readline()) != null) { do{ c =(char) br.read(); system.out.print(c); }while(c != ':'); system.out.println(); } } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } }
your code won't doing need anyway, because skip every 2nd line. instead:
try (bufferedreader br = new bufferedreader(new filereader("sales.dat"))) { string line; while ((line = br.readline()) != null) { int = line.indexof(':'); if (i != -1) system.out.println (line.substring(0, i)); } }
and infinite loop: because read() return -1 when @ end of file.
Comments
Post a Comment