Solved

How to print out all elements in HashTable

Posted on 2010-11-10
13
783 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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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 26

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

744 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now