2015-02-10 53 views
-3

我正在製作一個程序,該程序從文本文件中讀取一些數據,然後獲取該數據並找到數字的最小值,最大值和平均值。出於某種原因,我收到了很多我從未見過的荒謬錯誤。這裏是我的代碼:製作讀取和寫入數據的程序 - 然後計算最小值,最大值和平均值

import java.io.File; 
import java.util.Scanner; 
import java.io.FileWriter; 

public class Lab1 { 
static int count = 0; 
static int[] newData2 = new int[count]; 


// Method for reading the data and putting it into different arrays 
static int[] readData() { 
    File f = new File("data.txt"); 
    int[] newData = new int[100]; 
    try { 
     Scanner s = new Scanner(f); 
     while (s.hasNext()) { 
      newData[count++] = s.nextInt(); 

     } 

     for (int i = 0; i < newData2.length; i++) { 
      newData[i] = newData2[i]; 
      return newData2; 
     } 

    } catch (Exception e) { 
     System.out.println("Could not read the file."); 
    } 
} 

static int min(int[] newData2) { 
    int min = newData2[0]; 
    for (int i = 0; i < newData2.length; i++) { 
     if (newData2[i] < min) { 
      min = newData2[i]; 
     } 

    } 
    return min; 

} 

static int max(int[] newData2) { 
    int max = newData2[0]; 
    for (int i = 0; i < newData2.length; i++) { 
     if (newData2[i] > max) { 
      max = newData2[i]; 
     } 
    } 
    return max; 
} 

static double average(int[] newData2) { 
    double average = 0; 
    int sum = 0; 

    for (int i = 0; i < newData2.length; i++) { 
     sum = newData2[i]; 
    } 
    average = sum/newData2.length; 
    return average; 
} 

/* 
* static int stddev(int[] newData2) { int[] avgDif = new 
* int[newData2.length]; for(int i = 0; i < newData2.length; i++) { 
* avgDif[i] = (int) (average(newData2) - newData2[i]); } 
* 
* } 
*/ 

void write(String newdata, int min, int max, double average, int stddev) { 
    try { 
     File file = new File("stats.txt");  
     file.createNewFile(); 
     FileWriter writer = new FileWriter("stats.txt"); 
     writer.write("Minimum: " + min + "Maximum: " + max + "Average: " + average); 
     writer.close(); 
}catch(Exception e) { 
    System.out.println("Unable to write to the file."); 
} 

public static void main(String[] args) { 


} 

} 
} 

我在readData方法有一個錯誤,它告訴我說:

此方法必須返回INT []的結果類型。

我字面上返回一個int數組,所以我不明白這裏的問題是什麼。

然後在我的主要方法中,它說void是變量main的無效類型。

+1

「出於某種原因,我得到了很多可笑的錯誤,我以前從未見過的」既沒有我們。你應該補充你看到你的問題(與蹤跡)錯誤信息 – 2015-02-10 17:07:04

+0

已添加,對此抱歉。 – Logan 2015-02-10 17:09:42

+0

舉個例子,如果'new Scanner'引發異常,該方法不會返回 – 2015-02-10 17:10:21

回答

0

這裏有一些指針:

  1. 返回的東西必須返回的東西,如果線路new Scanner(f);拋出異常的方法的每個出口點,沒有達到第一回,所以你需要一個默認的,像此:
private int[] readData() { 
    File f = new File("data.txt"); 
    int count = 0; 
    int[] newData = new int[100]; 
    try { 
     Scanner s = new Scanner(f); 
     while (s.hasNext()) { 
      newData[count++] = s.nextInt(); // maybe you should handle the case where your input is too large for the array "newData" 
     } 
     return Arrays.copyOf(newData, count); 
    } catch (Exception e) { 
     System.out.println("Could not read the file."); 
    } 

    return null; 
} 
  • 爲了減少陣列的大小,應該使用Arrays.copyOf(見下文)
  • 你應該避免靜態字段(和你的情況都不是必需的)
  • 你的方法minmax都假設有數組(至少一個),你不應該這樣做,在元素(或與測試如果):
  • private int min(int[] data) { 
        int min = Integer.MAX_VALUE; // handy constant :) 
        for (int i = 0; i < data.length; i++) { 
         if (data[i] < min) { 
          min = data[i]; 
         } 
        } 
        return min; 
    } 
    
    private int max(int[] data) { 
        int max = 0; 
        for (int i = 0; i < data.length; i++) { 
         if (data[i] > max) { 
          max = data[i]; 
         } 
        } 
        return max; 
    } 
    
  • 在你average方法有幾個錯誤:
  • private double average(int[] data) { 
        int sum = 0; 
        for (int i = 0; i < data.length; i++) { 
         sum += data[i]; // here if you want a sum it's += not = 
        } 
        return (1.0 * sum)/data.length; // you want a double division, local "average" was useless 
    } 
    
  • 陣列iterables所以你可以使用 「新風格」 的循環:
  • for (int value : newData) { 
        // use value 
    } 
    

    一些閱讀:

    +0

    非常感謝!我對使用方法很陌生,所以他們一直困惑着我。這有助於我更好地理解它們。非常感謝!我現在開始了我的程序工作。 – Logan 2015-02-10 18:15:03

    0
    static int[] readData() { 
        File f = new File("data.txt"); 
        int[] newData = new int[100]; 
        try { 
         Scanner s = new Scanner(f); 
         while (s.hasNext()) { 
          newData[count++] = s.nextInt(); 
    
         } 
    
         for (int i = 0; i < newData2.length; i++) { 
          newData[i] = newData2[i]; 
          return newData2; 
         } 
    
        } catch (Exception e) { 
         System.out.println("Could not read the file."); 
        } 
    
        //TODO: return something here if there is some kind of error 
    } 
    

    由於try-catch塊,您需要考慮可能發生的每種可能性。當程序成功返回數組時,您希望返回數據,但當出現異常時,程序仍然會返回一個值,但是您沒有提供返回值。

    +0

    「由於try-catch塊」//對於upvote,如果try catch被刪除,你認爲他需要返回語句嗎? – 2015-02-10 17:27:05

    +0

    我修復了我的程序的其餘部分,並把我的'return newData2'放到了你建議的地方。現在唯一錯誤的是它不能讀取文件。在我的文件中有5個整數,並且s.hasNext()應該選取這些數字並將它們放入'newData [count ++]'數組中。是什麼導致這個混亂? – Logan 2015-02-10 17:28:06

    +0

    @RC如果我刪除了try catch塊,文件讀取器將無法工作。 – Logan 2015-02-10 17:30:11

    相關問題