Solved

How to print out all elements in HashTable

Posted on 2010-11-10
13
794 Views
Last Modified: 2012-08-14
I have an assignment asking me to create a hashtable then input all the string from a text file into the hashtable. And finally, print out all the element from hashtable.

to print out all the elements, I created the codes below:



Enumeration e = t.elements();
    while(e.hasMoreElements()){
      System.out.println((String)e.nextElement());
  }

Open in new window


Besides, I import:

import java.util.Enumeration;
import java.util.Hashtable;

Open in new window



However, it compiles error. The machine complaints it cannot find the elements() method.

I don't know why. Can somebody please help me?


Thanks

0
Comment
Question by:woeishyong
13 Comments
 
LVL 9

Expert Comment

by:the_b1ackfox
ID: 34108407
plenty of people can help you...   but you should post your code so we too can see why the code is erring out...
0
 
LVL 92

Expert Comment

by:objects
ID: 34108508
Sounds like t is not a Hashtable

>       System.out.println((String)e.nextElement());

that should also be:


      System.out.println(e.nextElement());
0
 

Author Comment

by:woeishyong
ID: 34108526
t is a Hashtable I created.


            HashTable t = new HashTable(0);


And I tried the other way,


Enumeration e = t.keys();
        while (e.hasMoreElements()) {
            String key = (String) e.nextElement();
            System.out.println(key);
        }


it's also compiles error. This time it cannot find method keys.

How can I fix it? Do I need to create a keys() / element() method ?
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 92

Expert Comment

by:objects
ID: 34108548
post all your code and i'll have a look
0
 

Author Comment

by:woeishyong
ID: 34108564
import java.io.FileInputStream;
import java.util.Scanner;
import java.util.Enumeration;
import java.util.Hashtable;

public class Hashtable {
public static void main(String[] args) throws Exception {
            
if( args.length != 1 ) return;

            HashTable t = new HashTable(0);
            
Scanner in1 = new Scanner(new FileInputStream(args[0]));
while( in1.hasNext()){
String s = in1.next();
int v = t.getvalue(s);
t.assign(s, v);
}

Enumeration e = t.elements();
    while(e.hasMoreElements()){
      System.out.println((String)e.nextElement());
  }

}
}


0
 
LVL 92

Accepted Solution

by:
objects earned 125 total points
ID: 34108614
> public class Hashtable {

you need to change the name of your class (and the filename)

eg.


public class HashtableTest {
0
 

Author Comment

by:woeishyong
ID: 34108668
Okok, then how can I print out?
0
 
LVL 92

Expert Comment

by:objects
ID: 34108721
same as you currently are
0
 

Author Comment

by:woeishyong
ID: 34108826
I tried, yet still not working. still compiles the same error. Cannot find the elements() method.


cannot find symbol
symbol  : method elements()
location: class HashTable
Enumeration e = t.elements();
0
 
LVL 92

Expert Comment

by:objects
ID: 34108833
>            HashTable t = new HashTable(0);

should be:


            Hashtable t = new Hashtable(0);
0
 
LVL 4

Expert Comment

by:ProgSysAdmin
ID: 34164954
The  t.getvalue(s);
and
t.assign(s, v);
Should give you compile errors as well.  Usually you would use:
t.put("one", 1);

instead.

Are you able to read from a text file ok?
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 37445782
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 106
even odd program using while loop 3 40
servlet example issue 6 39
What's wrong with this code? 4 23
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question