Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > BOOK: Beginning Java 2
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 19th, 2003, 07:22 AM
Registered User
 
Join Date: Oct 2003
Location: Thai Pao Lo, , .
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);
    }


  #2 (permalink)  
Old October 19th, 2003, 08:54 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What error message are you getting?

Martyn
  #3 (permalink)  
Old October 19th, 2003, 12:50 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
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
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 03:16 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.