Can someone help me to read a selective column of data in a text file into a list..
e.g.: if the text file data is as follows
-------------
id name a
java.util.Scanner
could be used to read the file, discarding the unwanted columns.
Either print the wanted column values as the file is processed or add()
them to a java.util.ArrayList
and print them once processing is complete.
A small example with limited error checking:
Scanner s = new Scanner(new File("input.txt"));
List names = new ArrayList();
// Skip column headings.
// Read each line, ensuring correct format.
while (s.hasNext())
{
s.nextInt(); // read and skip 'id'
names.add(s.next()); // read and store 'name'
s.nextInt(); // read and skip 'age'
}
for (String name: names)
{
System.out.println(name);
}