p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning Java 2 (http://p2p.wrox.com/forumdisplay.php?f=30)
-   -   array list sort (http://p2p.wrox.com/showthread.php?t=5152)

linfakngiau October 19th, 2003 07:22 AM

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();
         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();


      catch (FileNotFoundException exception)
         System.out.println ("The file " + file + " was not found.");
      catch (IOException exception)
         System.out.println (exception);


        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);


        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.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

Martyn October 19th, 2003 08:54 AM

What error message are you getting?


Martyn October 19th, 2003 12:50 PM

You haven't yet replied stating the description of the error message that you are getting, therefore I'm going to take a guess as to what I think might be the problem. Does the class AddressEntry implement the Comparable interface? If not, that is your problem.

See the following URL for some brief assistance:




All times are GMT -4. The time now is 12:14 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.