Bookmark Topic Watch Topic
  • New Topic

Calculating a number in a category  RSS feed

 
David Barry
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Report post to moderator
I have a slight problem here. I am writing a program that reads in a lot of hurricane data. As you can see in my code, I have read in information from the hurricane category array(this array keeps track of what category each hurricane was). I am trying to figure out how many (what number) of hurricanes there was in each category. So, I am wondering how I can calculate how many hurricanes are in each category. Does anybody know anything about this?

[edit]Add newlines. CR[/edit]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Report post to moderator
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...
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Report post to moderator
another comment... you may want to look into using some loops to print out your data. Seeing this:

repeated 58 time gives me a headache.

Couldn't you instead do something like

 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
David Barry
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi Fred, Thanks for the suggestion. However, I have about 59 hurricanes (I think), so if I set the int array to only 6 elements, that won't really work because I don't know how many Category 1 hurricanes I am going to have. I have to figure out how many of the 59 hurricanes will be Category 1's, 2's, and so on.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Report post to moderator
I had to edit your post and "break" some of the long lines with the ******************************** in; they were off the right edge of my screen. Please try to keep lines less than about 120 characters when coding because it makes them easier to read and print out.

I would add a couple of enhancements to Fred's very helpful suggestions.
  • When you are using a for loop for an array, use i < myArray.length for the middle element in the (). That ensures correct termination even if the size of the array changes.
  • You might do well to create a Hurricane object with all those data in, and a Hurricane[] array called storms.
  • If you override the toString method in Hurricane, you can print all your details out with a for-each loop . . .
  •  
    David Barry
    Ranch Hand
    Posts: 89
    • Mark post as helpful
    • send pies
    • Report post to moderator
    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...


    Thanks for this advice, Fred. However, in the line that says: "category = functionToCalculateCategory();", I am wondering what this line means. And how am I going to find out what the function is to calculate the category
     
    Ulf Dittmer
    Rancher
    Posts: 42972
    73
    • Mark post as helpful
    • send pies
    • Report post to moderator
    Is there a need to calculate the category? Isn't that what you're reading from file "hurcdata2category.txt"?
     
    David Barry
    Ranch Hand
    Posts: 89
    • Mark post as helpful
    • send pies
    • Report post to moderator
    ok. Then what purpose does this line of Fred's mean? "category = functionToCalculateCategory();"
     
    David Barry
    Ranch Hand
    Posts: 89
    • Mark post as helpful
    • send pies
    • Report post to moderator
    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.


    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
     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12563
    49
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Report post to moderator
    I didn't take the time to read your code very carefully. I don't know HOW you get the category - you could read it from a file, you could calculate it, you could get it as user input... The point is it doesn't matter.

    SOMEHOW, you get it. That's what this means:

    category = functionToCalculateCategory();

    SOMEHOW you know what the category is for the current hurricane you are processing, and use that as the index to your array.
     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12563
    49
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Report post to moderator
    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


    Let's back up to make sure we're all on the same page.

    Do you understand what an array is? Does this line make sense:

    int[] categoryCount = new int[6];
     
    David Barry
    Ranch Hand
    Posts: 89
    • Mark post as helpful
    • send pies
    • Report post to moderator
    Fred, I know what this means: "int[] categoryCount = new int[6];". It means an array of integers named categoryCount that has 6 elements. Additionally, I get the category information from a data file that I read into an array in my program. This data file tells the category (1-5) of each hurricane. So if that is what goes in that line, could you show me what you mean?
     
    David Barry
    Ranch Hand
    Posts: 89
    • Mark post as helpful
    • send pies
    • Report post to moderator
    Would I do it like this? Where "theCategory" is the array that contains the 1-5's.

     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12563
    49
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Report post to moderator
    You're close... I think you have most of the pieces, but we need to fit them together.

    This is correct for when you want to print out your array with the number of hurricanes of each category:



    However, you don't quite have building it right. You can't do this:

    hurricanNumByCat[theCategory]++;

    if 'theCategory' is an array. The only thing that can go inside those brackets is an integer - not an array of integers, as you are doing here.

    Look at this hunk of code:


    If i'm reading it right, you are here reading the file with the categories in it, and saving them to your 'theCategory' array. This saves the category number for each hurricane you're processing. What you need to do is to count the categories at the same time. You know that categoryCount is an array with five elements, and each one holds the count of how many category X hurricanes you have.

    so, let's say you read the first line of the file. Further, let's say it's a category 2 hurricane. you save the value in your 'theCategory' array. But now, you want to increment the counter for that category level:

    This shows us something interesting. I need to know what the value is and use it twice. I need to store it in theCategory[index], and i need to USE it to figure out which counter to increment. So, i'd probably do something like this:

     
    David Barry
    Ranch Hand
    Posts: 89
    • Mark post as helpful
    • send pies
    • Report post to moderator
    Ok. Thanks for the insight. However, I try that and get this: "[I@95da38". What in this code would cause that?


     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12563
    49
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Report post to moderator
    categoryCount is an Array. When you say "System.out.println(categoryCount);", java calls the toString() method on the Array, which basically prints the memory address of the array.

    you want to print the values IN the array. Please look at the beginning of my previous post.
     
    David Barry
    Ranch Hand
    Posts: 89
    • Mark post as helpful
    • send pies
    • Report post to moderator
    Fred, Thanks for all of the help today. I am leaving for today and come back to this program tomorrow. I believe I almost have it. Thanks again
     
      Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!