Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating new class question  RSS feed

 
Melinda Savoy
Ranch Hand
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am creating my first class within a webservice whereby I have to create a TRANSACTION ID (transid) and hold onto it until I complete the transaction and apply criteria to this ID and the transaction goes from web page to web page. I thought using a HASHTABLE would help me hang on to it but I am having a problem in that 1) I don't know if I am creating this correctly and 2) I am getting the following error:

SeverityDescriptionResourceIn FolderLocationCreation Time
2The method put(Object, Object) in the type Hashtable is not applicable for the arguments (int, float)Transaction.javaFoo/JavaSource/org/texasheath/wwwline 25June 29, 2005 12:13:14 PM


Here is my code:

package org.texasheath.www;
import java.util.*;

public class Transaction {
static int transid = 0;
static float transaction;

public static int CreateTransID(){

Hashtable h = new Hashtable();

int id = transid++;
h.put(id, transaction);
}
}

Any help or direction would appreciated. Regards.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This would actually work automatically in Java 5 (JDK 1.5) due to something called "autoboxing." In JDK 1.4 and older, you have to make it work by hand.

The problem is that the "put" method wants Objects as arguments, but "int" and "float" aren't Objects. What you can do is "wrap" your int and float using the standard wrapper classes "Integer" and "Float":



Note, though, that you have to remember the Hashtable contains Integer keys and Float objects, so when you want to get out, say, the transaction for id 123, you have to do

float transaction = ((Float) h.get(new Integer(123))).floatValue();
 
Melinda Savoy
Ranch Hand
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest,

Thanks so much for your help. I'll give it a whirl. I wish we were using 1.5 but IBM Rational only supports up to 1.5 currenlty per our rep.

Regards.
[ June 29, 2005: Message edited by: Melinda Savoy ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this real code or a throw-away example? Your HashMap is declared inside the method and will go away as soon as you exit the method. Maybe not what you wanted.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!