[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 833
  • Last Modified:

How to print out all elements in HashTable

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
woeishyong
Asked:
woeishyong
1 Solution
 
the_b1ackfoxCommented:
plenty of people can help you...   but you should post your code so we too can see why the code is erring out...
0
 
objectsCommented:
Sounds like t is not a Hashtable

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

that should also be:


      System.out.println(e.nextElement());
0
 
woeishyongAuthor Commented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
objectsCommented:
post all your code and i'll have a look
0
 
woeishyongAuthor Commented:
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
 
objectsCommented:
> public class Hashtable {

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

eg.


public class HashtableTest {
0
 
woeishyongAuthor Commented:
Okok, then how can I print out?
0
 
objectsCommented:
same as you currently are
0
 
woeishyongAuthor Commented:
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
 
objectsCommented:
>            HashTable t = new HashTable(0);

should be:


            Hashtable t = new Hashtable(0);
0
 
ProgSysAdminCommented:
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
 
mrcoffee365Commented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now