Storing file contents in a Collection

Hi All,

Im trying to read a csv file, break the lines by comma, then store them and sort them.

I have the test code working from: http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html

I am reading the file, but when I System.out.println() I keep getting the last line of the file only. Why is that?

try {

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

String line;

//Start reading the file while

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

// System.out.println(line);

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

//Counting tokens while

while (st.hasMoreTokens()){

count++;

//System.out.println(st.nextToken());

fullname = st.nextToken();

StringTokenizer wsp = new StringTokenizer(fullname, " ");

//breaking full name apart while

while(wsp.hasMoreTokens()){

fname = wsp.nextToken();

lname = wsp.nextToken();

}

email = st.nextToken();

phone = st.nextToken();

streetaddress = st.nextToken();

city = st.nextToken();

state = st.nextToken();

zip = st.nextToken();

contactArray = new Contact(fname, lname, email, phone, streetaddress, city, state, zip);

System.out.println(contactArray.toString());

}

}

List <Contact> contacts = Arrays.asList(contactArray);

Collections.sort(contacts);

System.out.println(contacts);

[1505 byte] By [iketurnaa] at [2007-11-27 6:36:20]
# 1

Well, in your code contactArray doesn't appear to be an array at all:

contactArray = new Contact(fname, lname, email, phone, streetaddress, city, state, zip);

And unless you're using an ancient JDK (pre 1.4) why not use String.split?

try {

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

List<Contact> contacts = new ArrayList<Contact>();

String line = null;

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

String[] parts = line.split(",");

if (parts.length < 7) throw new RuntimeException("Invalid contact info");

String[] fullName = parts[0].split(" ");

if (fullName.length < 2) throw new RuntimeException("Invalid name");

contacts.add(new Contact(fullName[0], fullName[1], parts[1],

parts[2], parts[3], parts[4], parts[5], parts[6]));

}

Collections.sort(contacts);

System.out.println(contacts);

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

dwga at 2007-7-12 18:03:54 > top of Java-index,Core,Core APIs...
# 2
This is so clearly written! Thank you!It seems like, at least to me, Collections can be studied for an entire summer!Question, are there many real world cases where reading from a csv is prefered over placing the csv file into a database?
iketurnaa at 2007-7-12 18:03:54 > top of Java-index,Core,Core APIs...
# 3

Hi,

im trying to sort by state then write to another file:

I keep getting an error on Collections.sort(contacts, new StateComparator());

Collections.sort(contacts, new StateComparator());

try {

BufferedWriter out = new BufferedWriter(new FileWriter("state-sort.csv"));

//PrintWriter p = new PrintWriter(out);

out.write(firstLine + "\n");

Iterator it = contacts.listIterator();

while(it.hasNext()) {

Object o = it.next();

out.write(o.toString() + "\n");

}

out.flush();

out.close();

}catch(FileNotFoundException ee) {}

catch(IOException ioe) {}

Here is my Comparator

package mybeans;

class StateComparator implements Comparable

{

String state;

public String getState() { return state; }

public int compare(Object obj1, Object obj2)

{

if(obj1 == obj2)

return 0;

Contact c1 = (Contact) obj1;

Contact c2 = (Contact) obj2;

return c1.getState().compareTo(c2.getState());

}

public int compareTo(Object obj)

{

Contact c = (Contact) obj;

return (state).compareTo(c.state + c.state);

}

}

Any help appreciated.

iketurnaa at 2007-7-12 18:03:54 > top of Java-index,Core,Core APIs...
# 4

Please use the code tags when posting code.

To answer your question:

Your StateComparator is implementing Comparable when it should be implementing Comparator. In my opinion it should look like this (assuming you are using JDK 1.5:

class StateComparator implements Comparator<Contact> {

@Override

public int compare(Contact c1, Contact c2) {

if (c1 == c2) return 0;

if (c1 == null && c2 == null) return 0;

if (c1 == null) return -1;

if (c2 == null) return 1;

// You might want to check for state's nullness aswell

return c1.getState().compareTo(c2.getState());

}

}

dwga at 2007-7-12 18:03:54 > top of Java-index,Core,Core APIs...