• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Creating new class question

 
Ranch Hand
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
What kind of corn soldier are you? And don't say "kernel" - that's only for this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic