2015-09-27 62 views
0

嘿,大家所以我必須做一個任務,我們通過一些天氣數據進行排序,我完成了大部分工作,但我沒有得到我應該得到的結果。 (編輯)這是一個文件鏈接:http://ksuweb.kennesaw.edu/~bsetzer/2302sp15/extra/data/636870-99999-2013.op
是我得到的樣子的結果:Java - 數據排序代碼,輸出不是它應該是的

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."); 


} } 
+2

不知道該文件的樣子有沒有一噸我們可以提供幫助。 –

+1

我不知道爲什麼'char fogIndicator = line2.getChars(132);'編譯,或者我應該說,它不會爲我編譯。 –

+0

它不編譯,但我的教授說,它應該和這裏是一個鏈接到數據文件http://ksuweb.kennesaw.edu/~bsetzer/2302sp15/extra/data/636870-99999-2013.op – Draco8573

回答

0

我忽略char fogIndicator = line2.getChars(132);,因爲它不會編譯,並沒有明顯的效果。

現在你的問題。在你的while循環你掃描下一行兩次,所以你不會掃描標題行。如果你在while循環之前移動第一個nextLine(),那麼一切都應該工作得很好!

你以前的代碼不會分析標題行,但也會跳過每一行。

實施例:

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

編輯: 這不固定的平均溫度。

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

相反,它比較你minTemp的,你應該把它比作你的avgMinTemp(minmt)。與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++; 
} 

最後你的fogindicator。而不是getChars();使用charAt(int pos);編譯並返回想要的字符。

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

希望這有助於:)

+0

那是固定的最大最大溫度和最小最小溫度。所以,謝謝你,現在我只需要弄清楚爲什麼其餘的都不起作用。 – Draco8573

+0

我當時齋戒,不是我。 平均值和霧日仍不正確。 – Iltis

+0

沒有老兄感謝您的快速反應。這確實解決了一些問題 – Draco8573

相關問題