Solved

Why doesn't my hashtable populate? Java

Posted on 2012-03-20
7
437 Views
Last Modified: 2012-03-20
I'm trying to pre-populate a Hashtable from a different class called ZipCodes

Here's my ZipCodes class with some test data:

import java.util.Hashtable;

public class ZipCodes {
	final static Hashtable<String, String> zipCodesList
    = new Hashtable<String, String>();
	public static void main() {

		zipCodesList.put("00501", "NY218");
		zipCodesList.put("00544", "NY218");
		zipCodesList.put("00601", "XX499");
		zipCodesList.put("31036", "GA076");
		zipCodesList.put("31037", "ZZ560");
		zipCodesList.put("31038", "ZZ740");
	        return;
	}

}

Open in new window


When I call upon ZipCodes in my code, it compiles fine, it just doesn't have data inside of it.

Here's what my call looks like:

Hashtable<String, String> zipCodesListing = ZipCodes.zipCodesList;
    	      cityName = zipCodesListing.get(cityCode);

Open in new window


The actual part where it does a .get(cityCode) works when the hashtable is being declared and populated in my main code.  But all it's returning is null.
0
Comment
Question by:Dale Harris
  • 4
  • 3
7 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37745368
Show how two pieces of your code actuall are comabined. when you execute the second snippet ?
0
 
LVL 16

Author Comment

by:Dale Harris
ID: 37745371
Yes.  It's based on a button push.  The class that I posted called ZipCodes is a standalone class in it's entirety.  That's all it does.  All the rest of my code is in my main java file.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37745372
This part will just execute, ppulate Hashatble and then exit

public static void main() {

		zipCodesList.put("00501", "NY218");
		zipCodesList.put("00544", "NY218");
		zipCodesList.put("00601", "XX499");
		zipCodesList.put("31036", "GA076");
		zipCodesList.put("31037", "ZZ560");
		zipCodesList.put("31038", "ZZ740");
	        return;
	}

Open in new window


- so when would you have a chance to say this:

Hashtable<String, String> zipCodesListing = ZipCodes.zipCodesList;
                cityName = zipCodesListing.get(cityCode);
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 47

Expert Comment

by:for_yan
ID: 37745375
No you can't do that - this class will execute and go away together with your Hashtable


You shuld do something like that - all from when you push the button
ZipCodes.populate();
Hashtable<String, String> zipCodesListing = ZipCodes.zipCodesList;
                cityName = zipCodesListing.get(cityCode);




public static void populate() {

		zipCodesList.put("00501", "NY218");
		zipCodesList.put("00544", "NY218");
		zipCodesList.put("00601", "XX499");
		zipCodesList.put("31036", "GA076");
		zipCodesList.put("31037", "ZZ560");
		zipCodesList.put("31038", "ZZ740");
	        
	}

Open in new window

0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 37745378
That should ber your class:

import java.util.Hashtable;

public class ZipCodes {
	final static Hashtable<String, String> zipCodesList
    = new Hashtable<String, String>();
	public static void populate() {

		zipCodesList.put("00501", "NY218");
		zipCodesList.put("00544", "NY218");
		zipCodesList.put("00601", "XX499");
		zipCodesList.put("31036", "GA076");
		zipCodesList.put("31037", "ZZ560");
		zipCodesList.put("31038", "ZZ740");
	        
	}

}
                                  

Open in new window



and thats how it acn be used in another class:

ZipCodes.populate();
Hashtable<String, String> zipCodesListing = ZipCodes.zipCodesList;
                cityName = zipCodesListing.get(cityCode);

Open in new window

0
 
LVL 16

Author Comment

by:Dale Harris
ID: 37745387
That was it!  Thanks.  Now I understand what you mean.  It's creating the variable because it's above the method.  But it wasn't doing the rest of it because it was not being called.

Thanks again!
0
 
LVL 16

Author Closing Comment

by:Dale Harris
ID: 37745388
Great, fast, easy solution!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

705 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

19 Experts available now in Live!

Get 1:1 Help Now