Wrox Programmer Forums
|
BOOK: Beginning Java 2
This is the forum to discuss the Wrox book Beginning Java 2, SDK 1.4 Edition by Ivor Horton; ISBN: 9780764543654
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Java 2 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 19th, 2003, 07:22 AM
Registered User
 
Join Date: Oct 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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);
    }


 
Old October 19th, 2003, 08:54 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What error message are you getting?

Martyn
 
Old October 19th, 2003, 12:50 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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:

http://developer.java.sun.com/develo...01/tt1023.html

Cheers

Martyn





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to sort an array ascending, help beccali Visual C++ 1 March 26th, 2007 05:19 PM
generic List<T> Sort() shadowcodes C# 2005 2 February 15th, 2007 01:30 PM
Sort by a value that's a comma seperated list medic XSLT 0 June 7th, 2006 09:52 AM
sort array of strings error lanita BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 1 December 14th, 2004 08:52 PM





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