File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Mapped Collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Mapped Collections" Watch "Mapped Collections" New topic
Author

Mapped Collections

Nick Hogue
Greenhorn

Joined: Feb 14, 2007
Posts: 4
Im having a hard time with this one.

1. Every state has a unique, two character state code. This program creates a HashMap and loads it with data for a few states. See if you can can fill-in the blanks to retrieve and display the data for one of the states based upon the value of the two character state code. If no such state code exists, the error message should be displayed.



import java.util.*;
public class Lab41A {
public static void main(String[] args) {
Map states = new HashMap();
states.put("CT", "Connecticut");
states.put("AZ", "Arizona");
states.put("IA", "Iowa");
states.put("NM", "New Mexico");
String stateCode = _______;
if (states._____________________(stateCode)) {
String stateName = __________________________________________;
System.out.println(stateCode + " - " + stateName);
}
else {
System.out.println("Invalid state code: " + stateCode);
}
}
}
Mike Mc Afee
Greenhorn

Joined: Jan 31, 2007
Posts: 19
Nick,

The theme of this forum is stated as "We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers."

I'm happy to help, but what is your question? This appears to be a homework assignment.

If I were to guess at a the point of the assignment, you are supposed to learn how to use the Java API to fill-in the blanks. You will probably find what you need by looking up the HashMap class at the Java SE5 API

Mike
[ February 14, 2007: Message edited by: Mike Mc Afee ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Hi, welcome to the ranch!

You'll find we don't give out a lot of answers without making you do the leg work first. Trust me, figuring it out yourself is much more valuable. And fun.

Have you spent some time with the Map interface in the JavaDoc? See if you can pick out the methods you need for a couple of those.

You could get StateCode from a couple different places. Let's say you put it on the command line when you start the program, like this:

Do you know where those arguments wind up so you can get one?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37941
    
  22
Welcome to the Ranch. We saw Lab40A a couple of days ago.

You know we don't give people answers to their homework assignments, but we do give them hints which might help them work it out.

We presume you are familiar with the API specifications website. You should get three frames; click HashMap in the lower left frame. Now go through the methods which HashMap has and you should be able to work out which of the methods goes into which of the blanks.
Then try it with stateCode = "CT"; and stateCode = "AK"; See what happens.

You will notice I have sent you to the J2SE1.4.2 documentation. You have been specified an old version of HashMap, so J2SE1.4.2 is more appropriate. I am afraid I can't find the older versions of the tutorial to refer you to; the later editions are here.

All the best.

CR
Nick Hogue
Greenhorn

Joined: Feb 14, 2007
Posts: 4
I am not looking for answer. I am trying to understand what is going on in the program. I am not sure what is being asked ? We are encouraged to get assitance from what ever sources are available. As many of you know... programming is built with trail and error. So far with this I am getting error. I dont understand the "string statecode =" line. I think I have figured the rest out . But I may be wrong.
[ February 14, 2007: Message edited by: Nick Hogue ]
Nick Hogue
Greenhorn

Joined: Feb 14, 2007
Posts: 4
Is this how it is suppose to look....

import java.util.*;
public class Lab41A {
public static void main(String[] args) {
Map states = new HashMap();
states.put("CT", "Connecticut");
states.put("AZ", "Arizona");
states.put("IA", "Iowa");
states.put("NM", "New Mexico");
String stateCode = "CT";
if (states.containsKey(new String(stateCode))) {
String stateName = stateCode;
System.out.println(stateCode + " - " + stateName);
}
else {
System.out.println("Invalid state code: " + stateCode);
}
}
}
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Your code looks OK, except from this line:

String stateName = stateCode;

Your program will now print something like this: CT - CT

That's not right. It should print something like this: CT - Connecticut

Instead of making stateName equal to stateCode, you're supposed to look up the state name in the map. Now, what should you change in that line to look up the name of the state?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
F. Jha
Greenhorn

Joined: Feb 16, 2007
Posts: 1
The code can be filled up like this too..

import java.util.*;
public class testMap {
public static void main(String[] args) {
Map states = new HashMap();
states.put("CT", "Connecticut");
states.put("AZ", "Arizona");
states.put("IA", "Iowa");
states.put("NM", "New Mexico");

String stateCode = "CT";
if (states.containsKey(stateCode)) {
String stateName =(String) states.get(stateCode);
System.out.println(stateCode + " - " + stateName);
}
else {
System.out.println("Invalid state code: " + stateCode);
}
}
}
 
 
subject: Mapped Collections
 
Similar Threads
Iteration
Null Pointer Execption error when running application
easiest way?
maps of classes of enums
S2: autocompleter - how to populate a list based on user's input?