array list sort
does anyone know how to sort with an array list I've tried I can compile it using Collection.sort but when I run it it gives an error message.
before that thank you very much for your kind assitance in my programming endevour
anyway here is the program
import java.util.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
public class AddressBook2 {
public static void main (String[] args)
{
JList list,list2,list3,list4;
DefaultListModel model,model2,model3,model4;
StringTokenizer tokenizer;
String line;
String addBook="";
String name,address,phnum,notes;
String file="Addressbook.nad";
int phone_num,search_num, count = 0;
ArrayList addList = new ArrayList();
try
{
FileReader fr = new FileReader (file);
BufferedReader inFile = new BufferedReader (fr);
line = inFile.readLine();
while (line != null)
{
tokenizer = new StringTokenizer (line,"~");
name = tokenizer.nextToken();
address = tokenizer.nextToken();
phnum = tokenizer.nextToken();
notes = tokenizer.nextToken();
addList.add(new AddressEntry(name,address,phnum,notes));
line = inFile.readLine();
}
inFile.close();
}
catch (FileNotFoundException exception)
{
System.out.println ("The file " + file + " was not found.");
}
catch (IOException exception)
{
System.out.println (exception);
}
Collections.sort(addList);
model = new DefaultListModel();
model2 = new DefaultListModel();
model3 = new DefaultListModel();
model4 = new DefaultListModel();
list = new JList(model);
list2 = new JList(model2);
list3 = new JList(model3);
list4 = new JList(model4);
for(int i=0;i<addList.size();i++)
{
Object b= addList.get(i);
model.addElement(((AddressEntry)b).getName());
model2.addElement(((AddressEntry)b).getAddress());
model3.addElement(((AddressEntry)b).getPhnum());
model4.addElement(((AddressEntry)b).getNotes());
}
JFrame frame = new JFrame ("List");
JLabel l1 = new JLabel ("Name");
JLabel l2 = new JLabel ("Address");
JLabel l3 = new JLabel ("Telephone Number");
JLabel l4 = new JLabel ("Notes");
frame.getContentPane().setLayout(new GridLayout(2,4,1,1));
frame.getContentPane().add (l1);
frame.getContentPane().add (l2);
frame.getContentPane().add (l3);
frame.getContentPane().add (l4);
frame.getContentPane().add (list);
frame.getContentPane().add (list2);
frame.getContentPane().add (list3);
frame.getContentPane().add (list4);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
|