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?

/**
 * The purpose of this program is to calculate the average category, 
 * pressure, and wind speed of all Atlantic Hurricanes which have 
 * occured from 1980-2006. It also tallys the number of storms in
 * each category,
 * 
 * @author John D. Barry 
 * @date   02/04/2009
 */
import java.util.Scanner;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.*;
public class Hurricanes2
{
    public static void main(String[] args) throws IOException
    {   
        int index = 0;
        int [] theYears = new int[59];
        File years = new File("hurcdata2years.txt");
        Scanner inFile = new Scanner(years);
        
        while (inFile.hasNextInt())
           {
            
                theYears[index] = inFile.nextInt();
                index++;
                

           }

        
        
        index = 0;
        int [] theWindspeed = new int[59];
        File windspeed = new File("hurcdata2windspeed.txt");
        inFile = new Scanner(windspeed);
        
        while (inFile.hasNextInt())
           {
            
                theWindspeed[index] = inFile.nextInt();
                index++;
                

           }

       
        
        index = 0;
        int [] thePressure = new int[59];
        File pressure = new File("hurcdata2pressure.txt");
        inFile = new Scanner(pressure);
        
        while (inFile.hasNextInt())
           {
            
                thePressure[index] = inFile.nextInt();
                index++;
                

           }


        
        index = 0;
        String [] theMonths = new String[59];
        File months = new File("hurcdata2months.txt");
        inFile = new Scanner(months);
        
        while (inFile.hasNext())
           {
            
                theMonths[index] = inFile.next();
                index++;
                

           }


        
        index = 0;
        String [] theNames = new String[59];
        File names = new File("hurcdata2names.txt");
        inFile = new Scanner(names);
        
        while (inFile.hasNext())
           {
            
                theNames[index] = inFile.next();
                index++;
                

           }

        
        
        index = 0;
        int [] theCategory = new int[59];
        File category = new File("hurcdata2category.txt");
        inFile = new Scanner(category);
        
        while (inFile.hasNextInt())
           {
            
                theCategory[index] = inFile.nextInt();
                index++;
                

           }
           
           int sumOne = 0;
           for (int i = 0; i < theCategory.length; i++) 
           {
           sumOne += theCategory[i];
           }
           double averageOne = sumOne / 59;
           
           int sumTwo = 0;
           for (int i = 0; i < thePressure.length; i++) 
           {
           sumTwo += thePressure[i];
           }
           double averageTwo = sumTwo / 59;
           
           int sumThree = 0;
           for (int i = 0; i < theWindspeed.length; i++) 
           {
           sumThree += theWindspeed[i];
           }
           double averageThree = sumThree / 59;
           

        
        
        System.out.println();
        System.out.println("                                                      Hurricanes 1980 - 2006");
        System.out.println();
        System.out.println("Year                   Hurricane                   Category                   Pressure                   Wind Speed" );
        System.out.println("********************************************************************************************************************");
        System.out.printf("%1d%25s%26d%28d%28d",theYears[0],theNames[0],theCategory[0],thePressure[0],theWindspeed[0]);
        System.out.println();
        System.out.printf("%1d%26s%25d%28d%28d",theYears[1],theNames[1],theCategory[1],thePressure[1],theWindspeed[1]);
        System.out.println();
        System.out.printf("%1d%25s%26d%28d%28d",theYears[2],theNames[2],theCategory[2],thePressure[2],theWindspeed[2]);
        System.out.println();
        System.out.printf("%1d%23s%28d%28d%28d",theYears[3],theNames[3],theCategory[3],thePressure[3],theWindspeed[3]);
        System.out.println();
        System.out.printf("%1d%25s%26d%28d%28d",theYears[4],theNames[4],theCategory[4],thePressure[4],theWindspeed[4]);
        System.out.println();
        System.out.printf("%1d%25s%26d%28d%28d",theYears[5],theNames[5],theCategory[5],thePressure[5],theWindspeed[5]);
        System.out.println();
        System.out.printf("%1d%26s%25d%28d%28d",theYears[6],theNames[6],theCategory[6],thePressure[6],theWindspeed[6]);
        System.out.println();
        System.out.printf("%1d%24s%27d%28d%28d",theYears[7],theNames[7],theCategory[7],thePressure[7],theWindspeed[7]);
        System.out.println();
        System.out.printf("%1d%24s%27d%28d%28d",theYears[8],theNames[8],theCategory[8],thePressure[8],theWindspeed[8]);
        System.out.println();
        System.out.printf("%1d%26s%25d%28d%28d",theYears[9],theNames[9],theCategory[9],thePressure[9],theWindspeed[9]);
        System.out.println();
        System.out.printf("%1d%27s%24d%28d%28d",theYears[10],theNames[10],theCategory[10],thePressure[10],theWindspeed[10]);
        System.out.println();
        System.out.printf("%1d%25s%26d%28d%28d",theYears[11],theNames[11],theCategory[11],thePressure[11],theWindspeed[11]);
        System.out.println();
        System.out.printf("%1d%28s%23d%28d%28d",theYears[12],theNames[12],theCategory[12],thePressure[12],theWindspeed[12]);
        System.out.println();
        System.out.printf("%1d%27s%24d%28d%28d",theYears[13],theNames[13],theCategory[13],thePressure[13],theWindspeed[13]);
        System.out.println();
        System.out.printf("%1d%24s%27d%28d%28d",theYears[14],theNames[14],theCategory[14],thePressure[14],theWindspeed[14]);
        System.out.println();
        System.out.printf("%1d%25s%26d%28d%28d",theYears[15],theNames[15],theCategory[15],thePressure[15],theWindspeed[15]);
        System.out.println();
        System.out.printf("%1d%23s%28d%28d%28d",theYears[16],theNames[16],theCategory[16],thePressure[16],theWindspeed[16]);
        System.out.println();
        System.out.printf("%1d%26s%25d%28d%28d",theYears[17],theNames[17],theCategory[17],thePressure[17],theWindspeed[17]);
        System.out.println();
        System.out.printf("%1d%25s%26d%28d%28d",theYears[18],theNames[18],theCategory[18],thePressure[18],theWindspeed[18]);
        System.out.println();
        System.out.printf("%1d%24s%27d%28d%28d",theYears[19],theNames[19],theCategory[19],thePressure[19],theWindspeed[19]);
        System.out.println();
        System.out.printf("%1d%24s%27d%28d%28d",theYears[20],theNames[20],theCategory[20],thePressure[20],theWindspeed[20]);
        System.out.println();
        System.out.printf("%1d%26s%25d%28d%28d",theYears[21],theNames[21],theCategory[21],thePressure[21],theWindspeed[21]);
        System.out.println();
        System.out.printf("%1d%24s%27d%28d%28d",theYears[22],theNames[22],theCategory[22],thePressure[22],theWindspeed[22]);
        System.out.println();
        System.out.printf("%1d%25s%26d%28d%28d",theYears[23],theNames[23],theCategory[23],thePressure[23],theWindspeed[23]);
        System.out.println();
        System.out.printf("%1d%26s%25d%28d%28d",theYears[24],theNames[24],theCategory[24],thePressure[24],theWindspeed[24]);
        System.out.println();
        System.out.printf("%1d%24s%27d%28d%28d",theYears[25],theNames[25],theCategory[25],thePressure[25],theWindspeed[25]);
        System.out.println();
        System.out.printf("%1d%27s%24d%28d%28d",theYears[26],theNames[26],theCategory[26],thePressure[26],theWindspeed[26]);
        System.out.println();
        System.out.printf("%1d%24s%27d%28d%28d",theYears[27],theNames[27],theCategory[27],thePressure[27],theWindspeed[27]);
        System.out.println();
        System.out.printf("%1d%25s%26d%28d%28d",theYears[28],theNames[28],theCategory[28],thePressure[28],theWindspeed[28]);
        System.out.println();
        System.out.printf("%1d%25s%26d%28d%28d",theYears[29],theNames[29],theCategory[29],thePressure[29],theWindspeed[29]);
        System.out.println();
        System.out.printf("%1d%24s%27d%28d%28d",theYears[30],theNames[30],theCategory[30],thePressure[30],theWindspeed[30]);
        System.out.println();
        System.out.printf("%1d%29s%22d%28d%28d",theYears[31],theNames[31],theCategory[31],thePressure[31],theWindspeed[31]);
        System.out.println();
        System.out.printf("%1d%26s%25d%28d%28d",theYears[32],theNames[32],theCategory[32],thePressure[32],theWindspeed[32]);
        System.out.println();
        System.out.printf("%1d%24s%27d%28d%28d",theYears[33],theNames[33],theCategory[33],thePressure[33],theWindspeed[33]);
        System.out.println();
        System.out.printf("%1d%27s%24d%28d%28d",theYears[34],theNames[34],theCategory[34],thePressure[34],theWindspeed[34]);
        System.out.println();
        System.out.printf("%1d%26s%25d%28d%28d",theYears[35],theNames[35],theCategory[35],thePressure[35],theWindspeed[35]);
        System.out.println();
        System.out.printf("%1d%27s%24d%28d%28d",theYears[36],theNames[36],theCategory[36],thePressure[36],theWindspeed[36]);
        System.out.println();
        System.out.printf("%1d%24s%27d%28d%28d",theYears[37],theNames[37],theCategory[37],thePressure[37],theWindspeed[37]);
        System.out.println();
        System.out.printf("%1d%26s%25d%28d%28d",theYears[38],theNames[38],theCategory[38],thePressure[38],theWindspeed[38]);
        System.out.println();
        System.out.printf("%1d%25s%26d%28d%28d",theYears[39],theNames[39],theCategory[39],thePressure[39],theWindspeed[39]);
        System.out.println();
        System.out.printf("%1d%26s%25d%28d%28d",theYears[40],theNames[40],theCategory[40],thePressure[40],theWindspeed[40]);
        System.out.println();
        System.out.printf("%1d%25s%26d%28d%28d",theYears[41],theNames[41],theCategory[41],thePressure[41],theWindspeed[41]);
        System.out.println();
        System.out.printf("%1d%25s%26d%28d%28d",theYears[42],theNames[42],theCategory[42],thePressure[42],theWindspeed[42]);
        System.out.println();
        System.out.printf("%1d%27s%24d%28d%28d",theYears[43],theNames[43],theCategory[43],thePressure[43],theWindspeed[43]);
        System.out.println();
        System.out.printf("%1d%25s%26d%28d%28d",theYears[44],theNames[44],theCategory[44],thePressure[44],theWindspeed[44]);
        System.out.println();
        System.out.printf("%1d%24s%27d%28d%28d",theYears[45],theNames[45],theCategory[45],thePressure[45],theWindspeed[45]);
        System.out.println();
        System.out.printf("%1d%27s%24d%28d%28d",theYears[46],theNames[46],theCategory[46],thePressure[46],theWindspeed[46]);
        System.out.println();
        System.out.printf("%1d%28s%23d%28d%28d",theYears[47],theNames[47],theCategory[47],thePressure[47],theWindspeed[47]);
        System.out.println();
        System.out.printf("%1d%24s%27d%28d%28d",theYears[48],theNames[48],theCategory[48],thePressure[48],theWindspeed[48]);
        System.out.println();
        System.out.printf("%1d%24s%27d%28d%28d",theYears[49],theNames[49],theCategory[49],thePressure[49],theWindspeed[49]);
        System.out.println();
        System.out.printf("%1d%25s%26d%28d%28d",theYears[50],theNames[50],theCategory[50],thePressure[50],theWindspeed[50]);
        System.out.println();
        System.out.printf("%1d%25s%26d%28d%28d",theYears[51],theNames[51],theCategory[51],thePressure[51],theWindspeed[51]);
        System.out.println();
        System.out.printf("%1d%24s%27d%28d%28d",theYears[52],theNames[52],theCategory[52],thePressure[52],theWindspeed[52]);
        System.out.println();
        System.out.printf("%1d%27s%24d%28d%28d",theYears[53],theNames[53],theCategory[53],thePressure[53],theWindspeed[53]);
        System.out.println();
        System.out.printf("%1d%27s%24d%28d%28d",theYears[54],theNames[54],theCategory[54],thePressure[54],theWindspeed[54]);
        System.out.println();
        System.out.printf("%1d%28s%23d%28d%28d",theYears[55],theNames[55],theCategory[55],thePressure[55],theWindspeed[55]);
        System.out.println();
        System.out.printf("%1d%26s%25d%28d%28d",theYears[56],theNames[56],theCategory[56],thePressure[56],theWindspeed[56]);
        System.out.println();
        System.out.printf("%1d%26s%25d%28d%28d",theYears[57],theNames[57],theCategory[57],thePressure[57],theWindspeed[57]);
        System.out.println();
        System.out.printf("%1d%25s%26d%28d%28d",theYears[58],theNames[58],theCategory[58],thePressure[58],theWindspeed[58]);
        System.out.println();
        System.out.println("********************************************************************************************************************");
        System.out.printf("Average" + "%48.0f%28.0f%28.0f",averageOne,averageTwo,averageThree);
        
        
        
        PrintWriter q = new PrintWriter(
                 new FileOutputStream("Summary.txt"), true);
                 
        q.println("                                                      Hurricanes 1980 - 2006");
        q.println();
        q.println("Year                   Hurricane                   Category                   Pressure                   Wind Speed" );
        q.println("********************************************************************************************************************");
        q.printf("%1d%25s%26d%28d%28d",theYears[0],theNames[0],theCategory[0],thePressure[0],theWindspeed[0]);
        q.println();
        q.printf("%1d%26s%25d%28d%28d",theYears[1],theNames[1],theCategory[1],thePressure[1],theWindspeed[1]);
        q.println();
        q.printf("%1d%25s%26d%28d%28d",theYears[2],theNames[2],theCategory[2],thePressure[2],theWindspeed[2]);
        q.println();
        q.printf("%1d%23s%28d%28d%28d",theYears[3],theNames[3],theCategory[3],thePressure[3],theWindspeed[3]);
        q.println();
        q.printf("%1d%25s%26d%28d%28d",theYears[4],theNames[4],theCategory[4],thePressure[4],theWindspeed[4]);
        q.println();
        q.printf("%1d%25s%26d%28d%28d",theYears[5],theNames[5],theCategory[5],thePressure[5],theWindspeed[5]);
        q.println();
        q.printf("%1d%26s%25d%28d%28d",theYears[6],theNames[6],theCategory[6],thePressure[6],theWindspeed[6]);
        q.println();
        q.printf("%1d%24s%27d%28d%28d",theYears[7],theNames[7],theCategory[7],thePressure[7],theWindspeed[7]);
        q.println();
        q.printf("%1d%24s%27d%28d%28d",theYears[8],theNames[8],theCategory[8],thePressure[8],theWindspeed[8]);
        q.println();
        q.printf("%1d%26s%25d%28d%28d",theYears[9],theNames[9],theCategory[9],thePressure[9],theWindspeed[9]);
        q.println();
        q.printf("%1d%27s%24d%28d%28d",theYears[10],theNames[10],theCategory[10],thePressure[10],theWindspeed[10]);
        q.println();
        q.printf("%1d%25s%26d%28d%28d",theYears[11],theNames[11],theCategory[11],thePressure[11],theWindspeed[11]);
        q.println();
        q.printf("%1d%28s%23d%28d%28d",theYears[12],theNames[12],theCategory[12],thePressure[12],theWindspeed[12]);
        q.println();
        q.printf("%1d%27s%24d%28d%28d",theYears[13],theNames[13],theCategory[13],thePressure[13],theWindspeed[13]);
        q.println();
        q.printf("%1d%24s%27d%28d%28d",theYears[14],theNames[14],theCategory[14],thePressure[14],theWindspeed[14]);
        q.println();
        q.printf("%1d%25s%26d%28d%28d",theYears[15],theNames[15],theCategory[15],thePressure[15],theWindspeed[15]);
        q.println();
        q.printf("%1d%23s%28d%28d%28d",theYears[16],theNames[16],theCategory[16],thePressure[16],theWindspeed[16]);
        q.println();
        q.printf("%1d%26s%25d%28d%28d",theYears[17],theNames[17],theCategory[17],thePressure[17],theWindspeed[17]);
        q.println();
        q.printf("%1d%25s%26d%28d%28d",theYears[18],theNames[18],theCategory[18],thePressure[18],theWindspeed[18]);
        q.println();
        q.printf("%1d%24s%27d%28d%28d",theYears[19],theNames[19],theCategory[19],thePressure[19],theWindspeed[19]);
        q.println();
        q.printf("%1d%24s%27d%28d%28d",theYears[20],theNames[20],theCategory[20],thePressure[20],theWindspeed[20]);
        q.println();
        q.printf("%1d%26s%25d%28d%28d",theYears[21],theNames[21],theCategory[21],thePressure[21],theWindspeed[21]);
        q.println();
        q.printf("%1d%24s%27d%28d%28d",theYears[22],theNames[22],theCategory[22],thePressure[22],theWindspeed[22]);
        q.println();
        q.printf("%1d%25s%26d%28d%28d",theYears[23],theNames[23],theCategory[23],thePressure[23],theWindspeed[23]);
        q.println();
        q.printf("%1d%26s%25d%28d%28d",theYears[24],theNames[24],theCategory[24],thePressure[24],theWindspeed[24]);
        q.println();
        q.printf("%1d%24s%27d%28d%28d",theYears[25],theNames[25],theCategory[25],thePressure[25],theWindspeed[25]);
        q.println();
        q.printf("%1d%27s%24d%28d%28d",theYears[26],theNames[26],theCategory[26],thePressure[26],theWindspeed[26]);
        q.println();
        q.printf("%1d%24s%27d%28d%28d",theYears[27],theNames[27],theCategory[27],thePressure[27],theWindspeed[27]);
        q.println();
        q.printf("%1d%25s%26d%28d%28d",theYears[28],theNames[28],theCategory[28],thePressure[28],theWindspeed[28]);
        q.println();
        q.printf("%1d%25s%26d%28d%28d",theYears[29],theNames[29],theCategory[29],thePressure[29],theWindspeed[29]);
        q.println();
        q.printf("%1d%24s%27d%28d%28d",theYears[30],theNames[30],theCategory[30],thePressure[30],theWindspeed[30]);
        q.println();
        q.printf("%1d%29s%22d%28d%28d",theYears[31],theNames[31],theCategory[31],thePressure[31],theWindspeed[31]);
        q.println();
        q.printf("%1d%26s%25d%28d%28d",theYears[32],theNames[32],theCategory[32],thePressure[32],theWindspeed[32]);
        q.println();
        q.printf("%1d%24s%27d%28d%28d",theYears[33],theNames[33],theCategory[33],thePressure[33],theWindspeed[33]);
        q.println();
        q.printf("%1d%27s%24d%28d%28d",theYears[34],theNames[34],theCategory[34],thePressure[34],theWindspeed[34]);
        q.println();
        q.printf("%1d%26s%25d%28d%28d",theYears[35],theNames[35],theCategory[35],thePressure[35],theWindspeed[35]);
        q.println();
        q.printf("%1d%27s%24d%28d%28d",theYears[36],theNames[36],theCategory[36],thePressure[36],theWindspeed[36]);
        q.println();
        q.printf("%1d%24s%27d%28d%28d",theYears[37],theNames[37],theCategory[37],thePressure[37],theWindspeed[37]);
        q.println();
        q.printf("%1d%26s%25d%28d%28d",theYears[38],theNames[38],theCategory[38],thePressure[38],theWindspeed[38]);
        q.println();
        q.printf("%1d%25s%26d%28d%28d",theYears[39],theNames[39],theCategory[39],thePressure[39],theWindspeed[39]);
        q.println();
        q.printf("%1d%26s%25d%28d%28d",theYears[40],theNames[40],theCategory[40],thePressure[40],theWindspeed[40]);
        q.println();
        q.printf("%1d%25s%26d%28d%28d",theYears[41],theNames[41],theCategory[41],thePressure[41],theWindspeed[41]);
        q.println();
        q.printf("%1d%25s%26d%28d%28d",theYears[42],theNames[42],theCategory[42],thePressure[42],theWindspeed[42]);
        q.println();
        q.printf("%1d%27s%24d%28d%28d",theYears[43],theNames[43],theCategory[43],thePressure[43],theWindspeed[43]);
        q.println();
        q.printf("%1d%25s%26d%28d%28d",theYears[44],theNames[44],theCategory[44],thePressure[44],theWindspeed[44]);
        q.println();
        q.printf("%1d%24s%27d%28d%28d",theYears[45],theNames[45],theCategory[45],thePressure[45],theWindspeed[45]);
        q.println();
        q.printf("%1d%27s%24d%28d%28d",theYears[46],theNames[46],theCategory[46],thePressure[46],theWindspeed[46]);
        q.println();
        q.printf("%1d%28s%23d%28d%28d",theYears[47],theNames[47],theCategory[47],thePressure[47],theWindspeed[47]);
        q.println();
        q.printf("%1d%24s%27d%28d%28d",theYears[48],theNames[48],theCategory[48],thePressure[48],theWindspeed[48]);
        q.println();
        q.printf("%1d%24s%27d%28d%28d",theYears[49],theNames[49],theCategory[49],thePressure[49],theWindspeed[49]);
        q.println();
        q.printf("%1d%25s%26d%28d%28d",theYears[50],theNames[50],theCategory[50],thePressure[50],theWindspeed[50]);
        q.println();
        q.printf("%1d%25s%26d%28d%28d",theYears[51],theNames[51],theCategory[51],thePressure[51],theWindspeed[51]);
        q.println();
        q.printf("%1d%24s%27d%28d%28d",theYears[52],theNames[52],theCategory[52],thePressure[52],theWindspeed[52]);
        q.println();
        q.printf("%1d%27s%24d%28d%28d",theYears[53],theNames[53],theCategory[53],thePressure[53],theWindspeed[53]);
        q.println();
        q.printf("%1d%27s%24d%28d%28d",theYears[54],theNames[54],theCategory[54],thePressure[54],theWindspeed[54]);
        q.println();
        q.printf("%1d%28s%23d%28d%28d",theYears[55],theNames[55],theCategory[55],thePressure[55],theWindspeed[55]);
        q.println();
        q.printf("%1d%26s%25d%28d%28d",theYears[56],theNames[56],theCategory[56],thePressure[56],theWindspeed[56]);
        q.println();
        q.printf("%1d%26s%25d%28d%28d",theYears[57],theNames[57],theCategory[57],thePressure[57],theWindspeed[57]);
        q.println();
        q.printf("%1d%25s%26d%28d%28d",theYears[58],theNames[58],theCategory[58],thePressure[58],theWindspeed[58]);
        q.println();
        q.println("********************************************************************************************************************");
    }
}

Recommended Answers

All 2 Replies

Well, you definitely need to replace all of those print statements with a loop that prints each line item from the array. There is absolutely no reason to type all of that in manually - it only differs by the array index.

I'd recommend writing a small data class (a struct if you have a c background) to hold the data for each of your entries. i.e.

class HurricaneEntry{
    int year;
    String name;
    int category;
    float pressure;
    float windspeed;
}

For your frequency count on the categories, a HashMap is useful.

Your code is an ideal candidate for:

1) A class: You have a whole bunch of attributes which you are storing in separate arrays. Make a class and make a single array of that class.

2) A loop: You have a bunch of print statements where you display information, about 60 or so, but they are displaying elements of the arrays. Shorten the code considerably by putting it in a loop.

3) Removal of code from main - Everything is in the main function. Create a class or class or classes and put the code in there. Your main function shouldn't have all of that in there. You have a Hurricanes2 class, but you don't do anything with it. You just have a big main function in it. Treat it as a class, create some class variables, etc.


Anything that can be categorized can be counted, it seems to me. You have a category variable called theCategory. It's hard to give much advice till the code is redesigned, but I'd have a Hurricane class with "category" as a data member of the class. You could have sub-classes of that, one for each type of Hurricane, but that may not be necessary. Set up some type of counter variable and when you create a new Hurricane object, increment the counter for that particular Hurricane category. Or have a Collection for each Hurricane category, like a Vector, which will do the counting for you. But I think a major redesign of the program is needed regardless of the counting, and that redesign should make it easy to count.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.