There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger wrote:if all you want is to keep track of how many hurricanes of each category, a simple numeric array is all you need.
Assume hurricanes can be any of category 1 - 5. I would declare an int array like this:
int [] hurricanNumByCat = new int[6];
that will give you an array with 6 elements, one of which you won't use (the 0th one).
Then, each time you calculate the category, increment that array counter:
When you are done, your array should have the totals. you can skip index 0:
Note: code not tested or verified, but it should give you the idea...
Ulf Dittmer wrote:Category is a number from 1 to 5, right? So you'd need five counters that get incremented whenever a hurricane in that category is read. Something like
int[] categoryCount = new int[6];
and later, in the loop that reads the file:
categoryCount[theCategory[index]]++;
Then the count for each category is at the indexes 1 through 5.
By the way, I'd avoid hardcoding the number "59" everywhere. Either make that a constant that gets declared once, or -much better- write the code in a way that is independent of the number of entries. As it is, the code breaks if there are either more or less than 59 entries.
Also, is there really no way to write all the print statement in a loop? It seems that you're hardcoding it for specific data, which makes it much harder to reuse later.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
David Barry wrote:
Ulf, Thanks for the help. I am trying this way that you recommended. However, I am getting a little lost. Could you explain yourself a little more with how I am supposed to do this? I get a little lost trying to follow you here. I think I am missing a step or something
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Don't get me started about those stupid light bulbs. |