Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Hashtable (http://www.velocityreviews.com/forums/t149628-hashtable.html)

tuurbo46@yahoo.co.uk 01-24-2006 05:57 PM

Hashtable
 
Hi

Im currently im in a bit of a corner. I am able to put data in a
hashtable, but now im in a muddle getting the data out.

How do i change the below method to retrive the data without using the
string?

// declared at top of source file
private TextField userName = new
TextField("Name:.","",20,TextField.ANY);
private TextField userNumber = new
TextField("Number:.","",20,TextField.ANY);

//Declaration of hashtable
Hashtable hT = new Hashtable(5);

//method in code
public void View()
{
Enumeration e1=hT.keys();
while(e1.hasMoreElements())
{
userName=(String)e1.nextElement();
userNumber=(String)hT.get(userName);
display.setCurrent(userNumber);
}
}


Oliver Wong 01-24-2006 06:12 PM

Re: Hashtable
 

<tuurbo46@yahoo.co.uk> wrote in message
news:1138125463.066937.5490@f14g2000cwb.googlegrou ps.com...
> Hi
>
> Im currently im in a bit of a corner. I am able to put data in a
> hashtable, but now im in a muddle getting the data out.
>
> How do i change the below method to retrive the data without using the
> string?
>
> // declared at top of source file
> private TextField userName = new
> TextField("Name:.","",20,TextField.ANY);
> private TextField userNumber = new
> TextField("Number:.","",20,TextField.ANY);
>
> //Declaration of hashtable
> Hashtable hT = new Hashtable(5);
>
> //method in code
> public void View()
> {
> Enumeration e1=hT.keys();
> while(e1.hasMoreElements())
> {
> userName=(String)e1.nextElement();
> userNumber=(String)hT.get(userName);
> display.setCurrent(userNumber);
> }
> }


If you don't care about the keys, use the values() method instead of the
keys() method to get an enumeration of values.

Are you using Java 1.5 or 1.4 or some other version?

- Oliver



zero 01-24-2006 06:15 PM

Re: Hashtable
 
tuurbo46@yahoo.co.uk wrote in news:1138125463.066937.5490
@f14g2000cwb.googlegroups.com:

> Hi
>
> Im currently im in a bit of a corner. I am able to put data in a
> hashtable, but now im in a muddle getting the data out.
>
> How do i change the below method to retrive the data without using the
> string?
>
> // declared at top of source file
> private TextField userName = new
> TextField("Name:.","",20,TextField.ANY);
> private TextField userNumber = new
> TextField("Number:.","",20,TextField.ANY);
>
> //Declaration of hashtable
> Hashtable hT = new Hashtable(5);
>
> //method in code
> public void View()
> {
> Enumeration e1=hT.keys();
> while(e1.hasMoreElements())
> {
> userName=(String)e1.nextElement();
> userNumber=(String)hT.get(userName);
> display.setCurrent(userNumber);
> }
> }
>
>


userName is a TextField, so why are you casting the retrieved element to
String? What's TextField anyway? java.awt.TextField has no constructor
that matches what you're using.

Using Hashtable is easy, you just have to make sure that what you put in
matches what you're trying to get out. You can't put in an Acorn and
expect to get an Oak back.

Hashtable table = new Hashtable();
table.put("1", new Integer(1));
table.put("2", new Integer(2));

for (Enumeration e = table.keys(); e.hasMoreElements(); )
{
String key = e.nextElement();
Integer value = (Integer)table.get(key);
System.out.println(key " => " value);
}


tuurbo46@yahoo.co.uk 01-24-2006 06:25 PM

Re: Hashtable
 
Hi

Im currently using jBuilder 9 and J2me within this enviroment. The
(System.out....) value is not used. Would i use the below code now?
Also the J2me enviroment does not like ( " => "), does anybody have any
ideas


for (Enumeration e = table.keys(); e.hasMoreElements(); )
{
String key = e.nextElement();
Integer value = (Integer)table.get(key);
display.setCurrent(key " => " value);
}


Oliver Wong 01-24-2006 06:56 PM

Re: Hashtable
 
<tuurbo46@yahoo.co.uk> wrote in message
news:1138127147.760278.190020@g43g2000cwa.googlegr oups.com...
> Hi
>
> Im currently using jBuilder 9 and J2me within this enviroment. The
> (System.out....) value is not used. Would i use the below code now?
> Also the J2me enviroment does not like ( " => "), does anybody have any
> ideas
>
>
> for (Enumeration e = table.keys(); e.hasMoreElements(); )
> {
> String key = e.nextElement();
> Integer value = (Integer)table.get(key);
> display.setCurrent(key " => " value);
> }
>


You should probably specify that you're using J2ME. If you don't say
anything, people might think you're using J2SE, and the libraries are
different between J2ME and J2SE.

The problem with " => " is that you need to add the + operator to do
string concatenation. Try something like this:

<code>
for (Enumeration e = table.keys(); e.hasMoreElements(); )
{
String key = e.nextElement();
Integer value = (Integer)table.get(key);
display.setCurrent(key + " => " + value);
}
</code>

- Oliver



zero 01-24-2006 07:12 PM

Re: Hashtable
 
"Oliver Wong" <owong@castortech.com> wrote in news:YLuBf.109771$AP5.28395
@edtnps84:

> The problem with " => " is that you need to add the + operator to do
> string concatenation.


duh on me

Roedy Green 01-24-2006 07:45 PM

Re: Hashtable
 
On 24 Jan 2006 09:57:43 -0800, tuurbo46@yahoo.co.uk wrote, quoted or
indirectly quoted someone who said :

>Im currently im in a bit of a corner. I am able to put data in a
>hashtable, but now im in a muddle getting the data out.


see http://mindprod.com/jgloss/hashtable.html
and http://mindprod.comj/jgloss/hashmap.html
which is the modern replacement.

for sample code.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green 01-24-2006 07:48 PM

Re: Hashtable
 
On 24 Jan 2006 10:25:47 -0800, tuurbo46@yahoo.co.uk wrote, quoted or
indirectly quoted someone who said :

> display.setCurrent(key " => " value);


You a NetRexx coder?
I wonder about the possibility of someday allowing either space or
some other than + character as the concatenation operator, perhaps _
or # or maybe even :
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Oliver Wong 01-24-2006 08:08 PM

Re: Hashtable
 

"Roedy Green" <my_email_is_posted_on_my_website@munged.invalid > wrote in
message news:911dt1t5d0ascmcjjdn0ng6ms6dip2dbet@4ax.com...
> On 24 Jan 2006 10:25:47 -0800, tuurbo46@yahoo.co.uk wrote, quoted or
> indirectly quoted someone who said :
>
>> display.setCurrent(key " => " value);

>
> You a NetRexx coder?
> I wonder about the possibility of someday allowing either space or
> some other than + character as the concatenation operator, perhaps _
> or # or maybe even :


A few languages use the period character. In a language I'm developing,
it's the ~ character. E.g. "Hello"~" world!"; my rational is that it's about
as difficult to type as the + character on a QWERTY keyboard (shift, and
then one of the two edges of the top most row), and the shape of the
character is vaguely reminiscent of using a string to tie two things
together.

- Oliver



Stefan Ram 01-24-2006 08:42 PM

Concatenation (was: Hashtable)
 
"Oliver Wong" <owong@castortech.com> writes:
>A few languages use the period character. In a language I'm
>developing, it's the ~ character. E.g. "Hello"~" world!"; my
>rational is that it's about


I am using in also in a language, I'm developing, namely
in Unotal, for the same purpose. [1]

I took this solution from Perl 6, where the tilde ~ also is
used for string concatenation.

[1]
Section "5.1" (with some typos) in
http://www.purl.org/stefan_ram/html/....html#anchor14



All times are GMT. The time now is 07:20 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.