使用计数数组来保持另一个数组的计数

时间:2014-03-31 17:43:16

标签: java arrays

我正在编写一个程序来读取天气数据的.txt文件,除其他外,还要记录一年中每天的各种天气模式。

.txt文件有365行不同的值(一年中每天的一行值)。每一行都包含高温,低温和天气状态(格式为六个1和0的String,其中1表示当天发生了雾/雨/雪/冰雹/雷霆/龙卷风。例如,String 011000表示雨和雪都会发生。

示例天气文件看起来像(间距的换行符):

高,低,天气字符串:

45,32.4,100000

43.2,35,100001

50.2,32.1 101101

我已经完成了绝大多数跟踪weather.txt文件的程序,并记录了每年的年度和年度低点,但是我很难统计每种类型的天气状态(6种类型)。我意识到我没有尽力解释,但我的目标是为所有365格式的String值保持计数,每个String的索引有多少1。因此,在查看上面的示例天气文件时,我的最终结果将是[3,0,1,1,0,2]。

在下面的粘贴方法中,我传入String array(我之前在程序中创建了一个String array,其中包含所有365个字符串格式的值... [100001,100000,101101 ,. ..])。在这个方法中,我创建了一个包含6个值的新tally array。我正在尝试编写一个跟踪传递的weather array(365值)的循环,如果在所述索引处出现1,则增加tally array index。最终的tally array看起来像[101,31,3,218,42,101] ......用于样本输出的数字。

逻辑给我带来了很多麻烦。假设我已经做了一些解释,我可以向我提供建议。

注意 - NUMBER_OF_WEATHER_TYPES的类常量设置为6.

public static int[] getWeatherCounts(String[] weather) {
    int[] tally = new int[6];

    for (int i = 0; i < weather.length; i++) {
        for (int j = 0; j < NUMBER_OF_WEATHER_TYPES; j++) {
            if (weather[j].charAt(j) == 1) {
                tally[j]++;
            }
            return tally;
        }
    }

    return tally;
}

上下文的整个程序:

import java.util.*;
import java.io.*;

public class WeatherInfo {    
public static final int DAYS_PER_YEAR = 365;
public static final int NUMBER_OF_WEATHER_TYPES = 6;

public static void main (String[] args) {
    String firstArgs = args[0];
    Scanner input = null;

    if (args.length != 1) {
        System.out.println("Error"); //Look more into this!!!!
    } else {
        try {
            input = new Scanner(new File(firstArgs));

        } catch (FileNotFoundException e) {
            System.out.println("Error: " + e);
            System.exit(1);
        }
    }

    String lineDiscard = input.nextLine();

    double[] highs = new double[DAYS_PER_YEAR];
    double[] lows = new double[DAYS_PER_YEAR];
    String[] weather = new String[DAYS_PER_YEAR];

    for (int i = 0; i < DAYS_PER_YEAR; i++) {
        input.next();
        input.next();
        highs[i] = input.nextDouble();
        lows[i] = input.nextDouble();
        weather[i] = input.next();
    }

    displayWeatherStatistics(highs, lows, weather);

}    

public static void displayWeatherStatistics(double[] highs, double[] lows, String[] weather) {
    double highTemp = Integer.MIN_VALUE;
    double lowTemp = Integer.MAX_VALUE;

    // for loop for highs
    for (int i = 0; i < highs.length; i++) {
        if (highs[i] > highTemp) {
            highTemp = highs[i];
        }
    }

    // for loop for lows
    for (int i = 0; i < lows.length; i++) {
        if (lows[i] < lowTemp) {
            lowTemp = lows[i];
        }
    }

    // printouts for the low and high temps of the year...need to fix this a bit
    System.out.println("Highest Temp: " + highTemp + " (F)");
    System.out.println("Lowest  Temp: " + lowTemp + " (F)");

    System.out.println(Arrays.toString(getWeatherCounts(weather)));


}

public static int[] getWeatherCounts(String[] weather) {
    int[] tally = new int[6];


    for (int i = 0; i < weather.length; i++) {
        for (int j = 0; j < NUMBER_OF_WEATHER_TYPES; j++) {
            if (weather[i].charAt(j) == 1) {
                tally[j]++;
            }
             return tally;
        }



    }

     return tally;
}

}

1 个答案:

答案 0 :(得分:2)

嗯,没有看到你的整个节目就很难说。但它看起来像我

if (weather[j].charAt(j) == 1) {
    tally[j]++;
}
return tally;

应该是

if (weather[i].charAt(j) == '1') {
    tally[j]++;
}
// omit the return tally, we don't want to do that until the end