Hashtable

T

tuurbo46

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);
}
}
 
O

Oliver Wong

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
 
Z

zero

(e-mail address removed) wrote in @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);
}
 
T

tuurbo46

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);
}
 
O

Oliver Wong

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
 
R

Roedy Green

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 :
 
O

Oliver Wong

Roedy Green said:
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
 
S

Stefan Ram

Oliver Wong said:
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/unotal.html#anchor14
 
T

tuurbo46

Hi

Thanks for you help guys. The dam thing still does not work. I think
my teacher should help more, but he comes out with the c...p line of,
degrees are read and not taught.

Anyway, does anybody know of a pay site where i can get some
programming problems solved (writen for me)?

Thanks again for help.
 
O

Oliver Wong

Hi

Thanks for you help guys. The dam thing still does not work. I think
my teacher should help more, but he comes out with the c...p line of,
degrees are read and not taught.

Anyway, does anybody know of a pay site where i can get some
programming problems solved (writen for me)?

Even if you go to the pay site, you will have to address these concerns
(which I feel you haven't done adequately yet):

What does your code look like?
What is your code supposed to do?
What's the difference between what it's doing now and what it's supposed to
do?

- Oliver
 
R

Roedy Green

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.

~ is already taken in Java. Pl/I's || is also taken.

~ would be a nice choice. It would not be confused with any other
operator if it weren't already taken and I agree it has the look of a
tie operator.

Perhaps when Unicode takes a foothold we might use \u2903 \u2908
\u2af6 \u2a6a \u2a1d \u25be for a concatenation operator.

see http://mindprod.com/jgloss/unicode.html to view the glyphs.
 
T

tuurbo46

Hi

Currently im doing some uni homework and we have to modify a mobile
phone application. This involves starting with a blank phone, and
adding the following items:

add entry
delete entry
view
search

So far i am able to get the main screen going (as above). With this
working i can select the add entry - this works fine. At this point i
add name and telephone number and this gets saved to a hashtable. At
this point i struggle because i cannot get data from hashtable to view
names and telephone numbers. Below is the list of my code snippets

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

//Hashtable
Hashtable hT = new Hashtable(10);


// adding people to hashtable

else if(event.equals("Save"))

{

hT.put(userName.getString(),userNumber.getString());

MainMenu();

}


// viewing people in hashtable - this does not work

public void View()

{

Enumeration e1=hT.keys();

while(e1.hasMoreElements())

{

userName = (String)e1.nextElement();

userNumber=(String)hT.get(userName);

display.setCurrent(userNumber);

}


//Fields for entering name and number

public void textBox()

{

display = Display.getDisplay(this);

Form f = new Form("Add:");

f.append(userName);

f.append(userNumber);

f.addCommand(backCommand);

f.addCommand(SaveCommand);

f.setCommandListener(this);

display.setCurrent(f);

}

}

Thanks guys.
 
T

Thomas Hawtin

Roedy said:
~ is already taken in Java. Pl/I's || is also taken.

Not necessarily a problem. + and - are used as both unary and binary
operators.

Tom Hawtin
 
O

Oliver Wong

Hi

Currently im doing some uni homework and we have to modify a mobile
phone application. This involves starting with a blank phone, and
adding the following items:

add entry
delete entry
view
search

So far i am able to get the main screen going (as above). With this
working i can select the add entry - this works fine. At this point i
add name and telephone number and this gets saved to a hashtable. At
this point i struggle because i cannot get data from hashtable to view
names and telephone numbers. Below is the list of my code snippets

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

//Hashtable
Hashtable hT = new Hashtable(10);


// adding people to hashtable

else if(event.equals("Save"))

{

hT.put(userName.getString(),userNumber.getString());

MainMenu();

}


// viewing people in hashtable - this does not work

public void View()

{

Enumeration e1=hT.keys();

while(e1.hasMoreElements())

{

userName = (String)e1.nextElement();

userNumber=(String)hT.get(userName);

display.setCurrent(userNumber);

}


//Fields for entering name and number

public void textBox()

{

display = Display.getDisplay(this);

Form f = new Form("Add:");

f.append(userName);

f.append(userNumber);

f.addCommand(backCommand);

f.addCommand(SaveCommand);

f.setCommandListener(this);

display.setCurrent(f);

}

}

Thanks guys.

Okay, now I have a much better understanding of what you are trying to
do. However, what do you mean by "this does not work"? What did you expect
to happen, and what is the code doing instead?

For example, "I expected for the user name to be displayed on the
screen, but instead I am getting a NullPointerException".

BTW, I notice there seems to be a closing brace missing for the method
view().

- Oliver
 
R

Roedy Green

Not necessarily a problem. + and - are used as both unary and binary
operators.

Even you could, my whole reason to avoid + is so that you don't use
the same symbol for two unrelated purposes even if the compiler can
figure it out.
 
T

tuurbo46

Hi

The missing bracket was a copy past error. The null pointer problem
has not happend on my enviroment. I cannot get it past the compiler.
The (string) in the view method shows red.

How would you now recommend i change the view method?
 
D

Damian Brunold

The (string) in the view method shows red.

If TextField is some kind of widget, then you most probably need to
do something like

while(e1.hasMoreElements())
{
userName.setText((String)e1.nextElement());
userNumber.setText((String)hT.get(userName));

instead of

while(e1.hasMoreElements())
{
userName=(String)e1.nextElement();
userNumber=(String)hT.get(userName);

Damian
 
O

Oliver Wong

Damian Brunold said:
If TextField is some kind of widget, then you most probably need to
do something like

while(e1.hasMoreElements())
{
userName.setText((String)e1.nextElement());
userNumber.setText((String)hT.get(userName));

instead of

while(e1.hasMoreElements())
{
userName=(String)e1.nextElement();
userNumber=(String)hT.get(userName);

Damian's suggestion here looks promising. But if it doesn't solve your
problem, perhaps you could give the exact error message that the compiler is
giving you?

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,842
Latest member
MableIwk73

Latest Threads

Top