Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to print out all elements in HashTable

Posted on 2010-11-10
13
798 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jdbc error in jsp application 20 63
expectj telnet failing 5 45
iterator/ListIterator approach 17 39
Java string replace 11 54
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

856 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