I’ve got a text file with dats for three people. The properties for each person is separated by a comma. It looks like this:
Patrick, Gary, Male, Blue, 1/28/1948
Carson, Larry, Male, Pink, 11/24/1976
Fisher, Paul, Male, Orange, 5/12/1995
What I’m trying to accomplish ultimately to to sort those people by their last name in ascending order.
Not sure I’m thinking about this the right way, but I wanted to create a 2D array that would assign each property to the proper row and column. We can forget about the sorting for now.
How can I get each line to populate a row of the array, with each separate property in its own column?
Maybe I should just be reading the file line by line, then adding each character to a 2D array using lineFromFile.charAt(index) to populate or something like that? Any sample suggestions would be greatly appreciated.
String.split()can be of help here:But what you probably really want is to abandon the idea of a 2-D array and create a List of Persons, where Person is a class that you define.
A 2D array almost always suggests that you need a class to represent a “row” of data.
Sorting becomes very easy too: