Java: Read from Multiple Lines (External File) -
i trying display contents of multiple rows in text file. can no problem single line, add line , i'm not sure need add code make move on next line. need myvalues[1] same myvalues[n] second line in file. believe need se new string next line i'm not sure how setup.
package a3; import java.io.*; public class a3 { public static void main(string [] args) { string animals = "animals.txt"; string line = null; try { filereader filereader = new filereader(animals); bufferedreader bufferedreader = new bufferedreader(filereader); string aline = bufferedreader.readline(); string myvalues[] = aline.split(" "); int n = 0; while((line = bufferedreader.readline()) != null) { system.out.println(myvalues[n] + " " + myvalues[1]); n++; } bufferedreader.close(); } catch(filenotfoundexception ex) { system.out.println("unable open file '" + animals + "'"); } catch(ioexception ex) { system.out.println("error reading file '" + animals + "'"); } } }
in code, array myvalues
never changed , contains values first line of text. need change each time new line, done in while
loop :
[...] while((line = bufferedreader.readline()) != null) { myvalues[] = line.split(" "); system.out.println(myvalues[n] + " " + myvalues[1]); n++; }
obviously not tested...
Comments
Post a Comment