• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

related to java.util.hashtable

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is code :
import java.util.*;
class abc
{
public static void main(String[] args)
{
Hashtable hm=new Hashtable();
for (int i=0;i<5;i++ )
{
hm.put("i","vipin"+String.valueOf(i));
System.out.println("Hello World!"+i+"\t"+hm.get("i"));
}
line-1System.out.println("Hello World!"+hm.get("i"));
line-2System.out.println("Hello World!"+hm.get("4"));
}
}
my question is i am using hashtable put and get methods for storing and retrieving values.i store values using "i" as integer variable in double quotes(b'cos method accept object)but when i retrieve values using variable "i" in line 1 it gives out put using i=4 but when i direct put the value of key i=4 as in line 2 it gives null value ?? why??
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i in quotes becomes a String "i". This is not perl it will not translate your variable withing quotes. In PERL you could have $i =2
and then "$i" = "2" in Java it does not work.


[This message has been edited by Aleksey Matiychenko (edited July 18, 2001).]
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Please Don't do it this way. You are not putting value of i while you do "i", It becomes a String (that is just 'i')...the correct way is...
hm.put(new Integer(i), "XYZ" + String.valueOf(i));
and to retrive plz use....
hm.get(new Integer(i));


------------------
Amit Agrawal,
New Delhi, India.
 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The key in your hashtable is actually a String object equal to "i". Since the hashtable must have a unique key, it overwrites the previous record, so you end up with one entry, key = "i" and value = 4. Instead of:
hm.put("i","vipin"+String.valueOf(i));
use:
hm.put(new Integer(i), "vipin"+String.valueOf(i));
This will make your key an Integer object with each unique value of i.
 
Attractive, successful people love this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic