Hashtable

Hi, I've been trying the hashtable of java codings,

Below are my codes for the sorting of the hashtable:

//Sort Values

public String returnSortedPairs(Hashtable information){

String strg = "";

Enumeration enumer = information.keys();

ArrayList keys = new ArrayList();

while( enumer.hasMoreElements() )

{

keys.add( enumer.nextElement() );

}

Collections.sort( keys );

Iterator i = keys.iterator();

while( i.hasNext() )

{

String myKey = (String) i.next();

String myValue = (String) information.get(myKey);

strg += myKey + " - " + myValue + "\n";

}

return strg;

}

May I know what would I have to input at

if (e.getSource() == btnSort)

If I would like that function of the GUI to perform the returnSortedPairs method? Sorry I'm really new at this GUI programming thing as also the Hashtable, I would appreciate any kind advises.

Thanks

[984 byte] By [victorxua] at [2007-11-27 4:07:14]
# 1

Aside: why not use a SortedMap?

import java.util.*;

public class SortedMapExample {

public static void main(String[] args) {

SortedMap < String, String > map = new TreeMap < String , String > ();

map.put("one","1");

map.put("two","2");

map.put("three","3");

map.put("four","4");

map.put("five","5");

map.put("six","6");

map.put("seven","7");

map.put("eight","8");

map.put("nine","9");

System.out.println(map);

}

}

DrLaszloJamfa at 2007-7-12 9:12:30 > top of Java-index,Java Essentials,Java Programming...
# 2
Thanks mate. :)Would you have any idea what might I have to put at if (e.getSource() == btnSort)to get the method to return a value?
victorxua at 2007-7-12 9:12:30 > top of Java-index,Java Essentials,Java Programming...
# 3
I'm not sure what you are trying to do. Post a small (< 1 page) example program that demonstrates your problem.
DrLaszloJamfa at 2007-7-12 9:12:30 > top of Java-index,Java Essentials,Java Programming...
# 4

Sorry for the confusion Dr.

Right now I have a hashtable called information

where i put Nric into as the Key ,

information.put (Nric, value);

I would like to employ a method to sort out the Nric in the hash table into Ascending order.

And also I would like the GUI button of mine btnClear to return the Sorted values of the Sort value method.

if (e.getSource == btnClear)

I have no idea how to get things to work

i'm so sorry for the many troubles

victorxua at 2007-7-12 9:12:30 > top of Java-index,Java Essentials,Java Programming...
# 5
What do you mean by "return the Sorted values of the Sort value method"?Return where?Again, can you post a short (<1 page) program that demonstrates what you are trying to say?
DrLaszloJamfa at 2007-7-12 9:12:30 > top of Java-index,Java Essentials,Java Programming...
# 6

This values are the addings to the hashtable information table:

//Add Function

public void addRec (Hashtable information) {

String Nric = txtNric.getText();

String name = txtName.getText();

String Tel = txtTel.getText();

while (Nric.length() != 9)

{

JOptionPane.showMessageDialog(null, "Please Enter valid NRIC length of 9", "Add Nric Number", JOptionPane.ERROR_MESSAGE);

Nric = JOptionPane.showInputDialog(null, "Please Enter Employee's Nric", "Add Nric value of Employee", JOptionPane.QUESTION_MESSAGE);

}

while (name.length() <= 0)

{

JOptionPane.showMessageDialog(null, "Please Enter Employee's Name!",

"Add Employee's Name", JOptionPane.ERROR_MESSAGE);

name = JOptionPane.showInputDialog(null, "Please Enter Employee's Name", "Add Telephone Number", JOptionPane.QUESTION_MESSAGE);

}

while (Tel.length() != 8)

{

JOptionPane.showMessageDialog(null, "Please Enter Telephone Number of Length 8",

"Add Telephone Number", JOptionPane.ERROR_MESSAGE);

Tel = JOptionPane.showInputDialog(null, "Please Enter Employee's Telephone Number", "Add Telephone Number", JOptionPane.QUESTION_MESSAGE);

}

String value = name+ "\t"+ Tel;

information.put (Nric, value);

JOptionPane.showMessageDialog(null,"Details of \nNRIC Number: " + Nric+ "\n Name: " +name+ "\n Telephone Number: " +Tel+ "\n Added to Record :)");

listAll(information);

}

What I would like is how should I go about sorting the hashtable in ascending order in respects to the Nric?

And how is it possible for me after having the sorted method have the function perform at the btnSort button?

I'm very sorry to be holding you up so late into the night Dr.

victorxua at 2007-7-12 9:12:30 > top of Java-index,Java Essentials,Java Programming...
# 7

> What I would like is how should I go about sorting the hashtable in ascending order in respects to the Nric?

Do you have to use Hashtable? if you use a sorted map like TreeNap, the entries

in the map are maintained in sorted order, as my first example demonstrated.

> And how is it possible for me after having the sorted method have the function perform at the btnSort button?

I'm sorry, I still don't understand this question. Is it a question about sorting, about event handling or something else?

DrLaszloJamfa at 2007-7-12 9:12:30 > top of Java-index,Java Essentials,Java Programming...
# 8
Yes I would have to use a hashtable sadly,I would like if (e.getSource == btnSort) to perform the sorting hashtable function, yes event handlingSo Sorry for the many confusions
victorxua at 2007-7-12 9:12:30 > top of Java-index,Java Essentials,Java Programming...