I have a text file with some information in it, which looks something like displayed below. I'm supposed to read the file after a specific word occurs (Complete Population), and store the vertically aligned values in each line like in an array (could be arraylist too)
What the file looks like -
Tue May 14 08:27:25 EST 2013
mating_pool=80
mutation_dist=3
algo_name=ARMO
Complete Population
8.78792396E8 7.45689508E8 8.37899916E8 9.52778502E8 8.47061622E8
8.80017166E8 7.50224432E8 8.23658404E8 9.51664198E8 8.49145008E8
8.85724416E8 7.48191542E8 7.61295532E8 1.00892758E9 8.52389824E8
8.96069156E8 7.11234404E8 7.68007126E8 9.7238065E8 8.5759227E8
8.96193522E8 7.11177522E8 7.67777526E8 9.72449466E8 8.5763106E8
8.95546766E8 7.1112849E8 7.68311754E8 9.71998374E8 8.57960886E8
8.95480802E8 7.11023308E8 7.68223532E8 9.72097758E8 8.5803376E8
8.9549393E8 7.11015392E8 7.68194136E8 9.72079838E8 8.5804897E8
8.95467666E8 7.11364074E8 7.68318732E8 9.7189094E8 8.58053462E8
8.95574386E8 7.11095656E8 7.68187948E8 9.71985272E8 8.58095624E8
8.95390774E8 7.11052654E8 7.684207E8 9.72098718E8 8.58105648E8
What I have tried
I'm able to read only one line of the numbers and not sure how to add numbers vertically.
Any help is appreciated.
Well, there actually is no issue here. You just need to code it. There are some nice pieces of code in this thread. Do something like this:
}
br.close();
Use a
BufferedReader
to wrap aFileReader
on the file, and then usenextLine()
to read each line. Create aPattern
object with regex".*Complete Population.*"
, and use aMatcher
on thatPattern
to check each line (looping with condition that theBufferedReader
'snextLine()
doesn't return null -- since null indicates end of file reached.) When a line matches, begin processing subsequent lines to form arrays.I'm not sure what you mean by "the vertically-aligned values", but if you mean the space-separated values on each line as an array, use
String.split("\\s+");
on each line to split on whitespace, returning an array of Strings.If by vertical arrays, you mean the first elements on each of the lines, then the second elements on each of the lines, and so on: You can store these arrays of Strings retrieved by
String.split("\\s+")
ing each line together as a 2-d array by placing each array into a main array which will hold them all (an array of arrays of per-line Strings), and then, when the full read-in is done and end of file is reached, go back to this 2-d array and access element [0] of each line to get a list of the first items on each line, element[1] of each line to get a list of the second items on each line, and so on. If you want, you can store these (effectively vertical lists of items on the lines) in another set of arrays.