Hashtable

Discussion in 'Java' started by tuurbo46@yahoo.co.uk, Jan 24, 2006.

  1. Guest

    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);
    }
    }
    , Jan 24, 2006
    #1
    1. Advertising

  2. Oliver Wong Guest

    <> wrote in message
    news:...
    > 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
    Oliver Wong, Jan 24, 2006
    #2
    1. Advertising

  3. zero Guest

    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);
    }
    zero, Jan 24, 2006
    #3
  4. Guest

    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);
    }
    , Jan 24, 2006
    #4
  5. Oliver Wong Guest

    <> wrote in message
    news:...
    > 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
    Oliver Wong, Jan 24, 2006
    #5
  6. zero Guest

    "Oliver Wong" <> 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
    zero, Jan 24, 2006
    #6
  7. Roedy Green Guest

    Roedy Green, Jan 24, 2006
    #7
  8. Roedy Green Guest

    On 24 Jan 2006 10:25:47 -0800, 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.
    Roedy Green, Jan 24, 2006
    #8
  9. Oliver Wong Guest

    "Roedy Green" <> wrote in
    message news:...
    > On 24 Jan 2006 10:25:47 -0800, 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
    Oliver Wong, Jan 24, 2006
    #9
  10. Stefan Ram Guest

    Concatenation (was: Hashtable)

    "Oliver Wong" <> 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/unotal.html#anchor14
    Stefan Ram, Jan 24, 2006
    #10
  11. Guest

    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.
    , Jan 24, 2006
    #11
  12. Oliver Wong Guest

    <> wrote in message
    news:...
    >
    > 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
    Oliver Wong, Jan 24, 2006
    #12
  13. Roedy Green Guest

    On Tue, 24 Jan 2006 20:08:28 GMT, "Oliver Wong" <>
    wrote, quoted or indirectly quoted someone who 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
    >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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Jan 24, 2006
    #13
  14. Guest

    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.
    , Jan 24, 2006
    #14
  15. Roedy Green wrote:
    >
    > ~ 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
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
    Thomas Hawtin, Jan 24, 2006
    #15
  16. Oliver Wong Guest

    <> wrote in message
    news:...
    > 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
    Oliver Wong, Jan 24, 2006
    #16
  17. Roedy Green Guest

    On Tue, 24 Jan 2006 23:14:16 +0000, Thomas Hawtin
    <> wrote, quoted or indirectly quoted someone
    who said :

    >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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Jan 25, 2006
    #17
  18. Guest

    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?
    , Jan 25, 2006
    #18
  19. > 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
    Damian Brunold, Jan 25, 2006
    #19
  20. Oliver Wong Guest

    "Damian Brunold" <> wrote in message
    news:...
    >> 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'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
    Oliver Wong, Jan 25, 2006
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guillermo

    Problem with hashTable

    Guillermo, Mar 4, 2004, in forum: Perl
    Replies:
    1
    Views:
    605
    Gunnar Hjalmarsson
    Mar 4, 2004
  2. Jonathan Wolfson

    vbc compilation fails when using Hashtable

    Jonathan Wolfson, Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    544
    Tu-Thach
    Jun 27, 2003
  3. John E

    Get Hashtable Object Directly

    John E, Oct 8, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    2,320
    Nicholas Paldino [.NET/C# MVP]
    Oct 8, 2003
  4. diya

    Type Hashtable not defined

    diya, Oct 31, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,771
    Nicole Calinoiu
    Oct 31, 2003
  5. D. Shane Fowlkes

    ArrayList versus HashTable

    D. Shane Fowlkes, Feb 12, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    17,784
    Kevin Spencer
    Feb 12, 2004
Loading...

Share This Page