Java - 数据排序代码,输出不是应该的

时间:2015-09-27 18:30:29

标签: java

嘿伙计,所以我必须做一项任务,我们对一些天气数据进行了分类,我完成了大部分工作,但我没有得到应有的结果。 (编辑)以下是文件的链接:http://ksuweb.kennesaw.edu/~bsetzer/2302sp15/extra/data/636870-99999-2013.op 我得到的结果如下:

Station ID: 636870      
WBAN ID: 99999      
Year: 2013      
Max average temp: 68.0      
Min average temp: 63.1       
Max max temp: 89.6      
Min min temp: 51.8      
Average visibility: 73.18993288590602      
149 lines counted.      

但是输出应该是这样的。

Station id:          636870      
WBAN id:             99999      
Year:                2013      
Max Average Temp:    78.6      
Min Average Temp:    60.8       
Max Max Temp:        91.4      
Min Min Temp:        51.8      
Average visibility:  6.535507246376822      
Number of fog days:  4     

我的代码:

 public static void main(String[] args){

    File infile = new File("636870-99999-2013.op");
    int totalLineCount = 0;
    int stnNumber = 0;
    int wbanNumber = 0;
    int year = 0;
    double meanTemp = 0;
    double mtemp = 0;
    double minmt = 1000;
    double maxmt = 0;
    double maxTemp = 0;
    double mtemp2 = 0;
    double minTemp = 1000;
    double mtemp3 = 0;
    double aV = 0;
    int count = 0;
    double sum = 0.0;

    try {
        Scanner inscan = new Scanner(infile);



        while (inscan.hasNext()) {

            String line = inscan.nextLine();
            String line2 = inscan.nextLine();
            //STN Number
            String stnNumberField = line2.substring(0, 6);
            String stnNumberTrimmed= stnNumberField.trim();
            stnNumber = Integer.parseInt(stnNumberTrimmed);

            //wban number
            String wbanNumberField = line2.substring(7, 12);
            String wbanNumberTrimmed = wbanNumberField.trim();
            wbanNumber = Integer.parseInt(wbanNumberTrimmed);

            //year
            String yearField = line2.substring(14, 18);
            String yearFieldTrimmed = yearField.trim();
            year = Integer.parseInt(yearFieldTrimmed);

            //average temp
            String meanTempField = line2.substring(24, 30);
            String meanTempFieldTrimmed = meanTempField.trim();
            mtemp = Double.parseDouble(meanTempFieldTrimmed);
            //min average temperature
            if (mtemp != 99.99 && mtemp != 999.9 && mtemp != 9999.9){
                if( mtemp < minTemp){
                    minmt = mtemp;
                }
            }
            if (mtemp != 9999.9){
                if (mtemp > maxTemp){
                    maxmt = maxTemp;
                }
            }


            //max temp
            String maxTempField = line2.substring(102, 108);
            String maxTempFieldTrimmed = maxTempField.trim();
            mtemp2 = Double.parseDouble(maxTempFieldTrimmed);
            if (mtemp2 != 9999.9){
                if (mtemp2 > maxTemp){
                    maxTemp = mtemp2;
                }
            }

            //min temp
            String minTempField = line2.substring(110, 116);
            String minTempFieldTrimmed = minTempField.trim();
            mtemp3 = Double.parseDouble(minTempFieldTrimmed);
            if (mtemp3 != 99.99 && mtemp3 != 999.9 && mtemp3 != 9999.9){
                if( mtemp3 < minTemp){
                    minTemp = mtemp3;
                }
            }

            //average visibility
            String averageVisibility = line2.substring(68, 73);
            String averageVisibilityTrimmed = averageVisibility.trim();
            aV = Double.parseDouble(averageVisibilityTrimmed);
            // read data and compute statistics
                double value = aV;
                sum += value;
                count++;


            //fog indicator
            char fogIndicator = line2.getChars(132);



            totalLineCount++;
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    double average = sum / count;

    System.out.println("Station ID: " + stnNumber);
    System.out.println("WBAN ID: " + wbanNumber);
    System.out.println("Year: " + year);
    System.out.println("Max average temp: " + maxmt);//why is this incorrect?
    System.out.println("Min average temp: " + minmt);//why is this incorrect?
    System.out.println("Max max temp: " + maxTemp); //why is this output incorrect?
    System.out.println("Min min temp: " + minTemp);
    System.out.println("Average visibility: " + average);
    //Number of fog days:  11

    System.out.println(totalLineCount + " lines counted." );


} }

1 个答案:

答案 0 :(得分:0)

我忽略了char fogIndicator = line2.getChars(132);因为它无法编译而且没有明显效果。

现在问题。在 while-loop 中,您扫描下一行两次,这样就不会扫描标题行。如果你在while循环之前移动第一个nextLine(),一切都应该正常工作!

您以前的代码不会解析标题行,但也会跳过每隔一行。

示例:

String line = inscan.nextLine();
while (inscan.hasNext()) {
    String line2 = inscan.nextLine();
    //...

修改 这并不能解决平均温度。

if (mtemp < minTemp) {
    minmt = mtemp;
//...
if (mtemp > maxTemp) {
    maxmt = maxTemp;

您应该将它与您的avgMinTemp(minmt)进行比较,而不是将其与您的minTemp进行比较。与avgMaxTemp类似的问题。

if (mtemp < minmt ) {
    minmt = mtemp;
//...
if (mtemp > maxmt) {
    maxmt = mtemp;

要修复您的avgVisibility,您只需添加您在此行忘记的过滤器语句。

if (value != 99.99 && value != 999.9 && value != 9999.9) {
    sum += value;
    count++;
}

最后你的迷雾指示器。而不是getChars();使用charAt(int pos);它编译并返回想要的角色。

char fogIndicator = line2.charAt(132);
if(fogIndicator == '1')
    fog++;

希望这会有所帮助:)