java - How to split the strings in a file and read them? -
i have file information in it. looks like:
michael 19 180 miami george 25 176 washington william 43 188 seattle
i want split lines , strings , read them. want like:
michael 19 180 miami george ...
i used code this:
bufferedreader in = null; string read; int linenum; try{ in = new bufferedreader(new filereader("fileeditor.txt")); } catch (filenotfoundexception e) {system.out.println("there problem: " + e);} try{ (linenum = 0; linenum<100; linenum++){ read = in.readline(); if(read == null){} else{ string[] splited = read.split("\\s+"); system.out.println(splited[linenum]); } } } catch (ioexception e) {system.out.println("there problem: " + e);} }
what gave me
michael 25 188
i think issue loop i'm not advanced in programming , i'll appreciate help. thanks.
you're part way there great.
when reading file, reader
return null
when reaches end of stream, meaning nothing else available read. current approach means want read @ least 100 lines, no more...this become problematic in future if file size increases...it's wasteful
instead, should use fact null
value indicates end of file..
when split line, contain number of elements. using linenum
variable print these. problem is, you've read , split line, linenum
irrelevant task, represents number of lines you've read, not part of string you've split.
instead, need use inner loop display individual split elements each line...
for example...
bufferedreader in = null; try { in = new bufferedreader(new filereader("fileeditor.txt")); string read = null; while ((read = in.readline()) != null) { string[] splited = read.split("\\s+"); (string part : splited) { system.out.println(part); } } } catch (ioexception e) { system.out.println("there problem: " + e); e.printstacktrace(); } { try { in.close(); } catch (exception e) { } }
also, don't forget, if open it, musty close ;)
you might want take little more time going through basic i/o ;)
Comments
Post a Comment