[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to print out all elements in HashTable

Posted on 2010-11-10
13
Medium Priority
?
827 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
[X]
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
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
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!

 
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 500 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

649 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