aspose file tools*
The moose likes Beginning Java and the fly likes easiest way? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "easiest way?" Watch "easiest way?" New topic
Author

easiest way?

Charles Saik
Greenhorn

Joined: Sep 07, 2007
Posts: 8
I have an input file that contains a bunch of records. Each record has a state abbreviation and several counts of various things.

My program is setup to read in the file and process the counts as a whole, and it works great. But what I need to do now is process the counts by state as well.

I figured that I would make a two-dimensional array, with the first dimension indicating the 50 states, and the second dimension to hold the accumulated counts for each state.

The trouble is, the state abbreviations are strings, and the counts are integers. I could probably use int constants to indicate each state, but I'm not sure if this is the easiest / most effective way to do what I want.

Any suggestions?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

you probably want to use a Map of some kind. this will let you store pairs - one (the state as a string) is they key value, and the other is the data.

in your case, you'd simply check to see if the key exists in the map. if so, increment the current value. if not, create the key with whatever initial value you have.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2289
    
  49
If you are going to have to trawl the array to find the matching state for every record you read in you'd be better off using a Map such as HashMap. Maps are much more efficient at finding data than having to search an array each time. You could use the state string as the key and create a simple local mutable integer class (to hold the count) as the value.
Charles Saik
Greenhorn

Joined: Sep 07, 2007
Posts: 8
Thanks all.

I've never used a HashMap before... can someone show me how to create a simple one?
Adam Nace
Ranch Hand

Joined: Jul 17, 2006
Posts: 117
I think I would recommend making an enum, and using an enum map. Although 50 is awefully big for an enum, it'll make your code cleaner, because if you find a name isn't a state, it'll get caught by the valueOf method. This should be relatively safe, because the fifty states aren't likely to change anytime soon.

- Adam
Eric Duval
Greenhorn

Joined: Sep 12, 2007
Posts: 3
import java.util.TreeMap;
public class TreeMapTest {

public static void main (String args[]) {
TreeMap<String, Integer> theMap = new TreeMap<String, Integer>();

theMap.put("NY", 1);
theMap.put("CA", 1);


String state = "CA";
if(theMap.containsKey(state)) {
int count = theMap.get(state) + 1;
theMap.put(state, count);
}

System.out.println("California was found " + theMap.get("CA") + " times.");
System.out.println("Here is the full map:\n " + theMap.toString());
}
}

===============
OUTPUT:
===============

California was found 2 times.
Here is the full map:
{CA=2, NY=1}
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You mentioned "several counts of various things". In situations like that it's often worth making a custom class to represent one row.

Then you can do something like

I used lots of bad shortcuts there ... public variables, didn't show a constructor, etc. Does the idea look good for your problem?


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
Charles Saik
Greenhorn

Joined: Sep 07, 2007
Posts: 8
Thanks a bunch. That looks very interesting. I'm going to have to check it out.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: easiest way?