Sorting data from a csv file

Hi All,

I currently can read a csv file into my console using:

BufferedReader in = new BufferedReader(new FileReader("/Users/Admin/Desktop/list.csv"));

int numberOfRows = 0;

int numberOfColumns = 0;

String line;

while ((line = in.readLine()) != null) {

System.out.println(line);

if(line!= null){

numberOfRows++;

StringTokenizer st = new StringTokenizer(line, ",");

numberOfColumns= st.countTokens();

}

}

System.out.println("Number of Rows: " + numberOfRows);

System.out.println("Number od Columns: " + numberOfColumns);

in.close();

}

My output:

Full Name,Email,Phone,Street Address,City,State,Zip

James Madison,jmadison@whitehouse.gov,201-555-1234,12 Main Street,Newark,NJ,11346

theodore roosevelt, teddy@whitehouse.gov, 415-555-1234, 1239 Geary, San Francisco, CA,95412

Franklin Roosevelt, fdr@whitehouse.gov, 718-555-1234, 805 New York Avenue, Brooklyn, NY,11257

Abe Lincoln, abe@whitehouse.gov, 312-555-1234, 22 Michigan Avenue, Chicago, IL,35124

Ronald Reagan, rreagan@whitehouse.gov, 261-555-1234, 14 Main Street, Columbus, OH,35214

bill clinton, bigbill@hotmail.com, 914-555-1234, 128 Readers Digest Road, Chappaqua, NY,10538

Number of Rows: 7

Number od Columns: 7

How can I sort this by ful name, then write to a file, then sort by state, then write to another file?

Any help would be appreciated.

ike

[1495 byte] By [iketurnaa] at [2007-11-27 6:15:58]
# 1

looks like u were reading the data directly from a file and then output them using System.out.println().

u can sort them, but first u'll need to put each of those data into a java bean class, and then create a List object (i.e. ArrayList) to store each of these java bean classes. use the Collections class to sort the list.

hope this help

JWKC-5ivea at 2007-7-12 17:27:11 > top of Java-index,Core,Core APIs...
# 2
must man absolute use bean class ?i think ,that it works too only with List ,doesn't it?
cuongdeutsch2@yahoo.coma at 2007-7-12 17:27:11 > top of Java-index,Core,Core APIs...
# 3

yes u can use a List to store each of the record (as below):

"bill clinton, bigbill@hotmail.com, 914-555-1234, 128 Readers Digest Road, Chappaqua, NY,10538"

u can also sort the list, but u can only sort it as a whole, i.e. u cannot sort the list by the address, or post code, whereas if u store each of the field into instance variables inside a java bean, u can then use the getter method to extract data from the list record.

JWKC-5ivea at 2007-7-12 17:27:11 > top of Java-index,Core,Core APIs...
# 4

> must man absolute use bean class ?i think ,that it

> works too only with List ,doesn't it?

Bean class or not, you need to think about a proper representation of your data. A String clearly isn't, so you need a custom object to contain your data as separate fields.

I don't know why you would think that a "bean class" would only work with List, but it doesn't. You will probably want to override the equals() and hashCode() methods in your new custom class.

Herko_ter_Horsta at 2007-7-12 17:27:11 > top of Java-index,Core,Core APIs...