2013-05-05 85 views
0

我是一個初學者在java,我很努力讓這個代碼工作。我正在嘗試讀取CSV文件,然後使用該數據計算平均值,然後返回最高平均值和最高平均值的摘要。Java閱讀文件/掃描器

輸入如下:

艾麗西亞商標,89,90,100,95,75,85,94,100,90,92

鮑比·裏克斯,98,79,87,79,9 ,98,7,19,98,78

這裏是放出來應該是什麼樣子(數字是不正確的,例如只):

艾麗西亞商標85.5乙

鮑比·裏克斯90.0 A-

...

高98.2

低56.5

平均78.3333

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.ArrayList; 
import java.util.Scanner; 

public class Test 
{ 

    public static void main(String[] args) 
    { 
     Test x = new Test(); 
     x.high(); 
    } 

    public void high() 
    { 
     File file2 = new File("scores.csv"); 
     try 
     { 
      Scanner scanner = new Scanner(file2); 
      while (scanner.hasNextLine()) 
      { 
       String line2 = scanner.nextLine(); 
       String[] value2 = line2.split(","); 

       // Converts String Array into Double Array 
       double[] score = new double[value2.length]; 
       for (int i = 1; i < value2.length; i++) 
       { 
        score[i] = Double.parseDouble(value2[i]); 
       } 

       // Finds the sum and then the average, adds it to a array List 
       ArrayList<Double> av; 
       av = new ArrayList<Double>(); 
       double sumNum = 0.0; 
       for (double i : score) 
       { 
        sumNum += i; 
       } 
       double aver = sumNum/10; 
       av.add(aver); 

       double max = 0, min = 100; 
       for (int a = 0; a < av.size(); a++) 
       { 
        double s = av.get(a); 
        max = Math.max(max, s); 
        min = Math.min(min, s); 
        aver += s; 
       } 

       System.out.println("High " + max + ""); 
       System.out.println("Low " + min + ""); 
       System.out.println("Average " + aver/av.size() + ""); 

      } 
      scanner.close(); 
     } 
     catch (FileNotFoundException e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 

我覺得我有問題是與while循環,但我不是太確定了。任何幫助解決這個問題將非常感激。

+2

你告訴我們你的需求,你告訴我們的代碼,但你不要告訴我們你的代碼有什麼問題,這使我們很難幫助你。請告訴我們您的代碼出了什麼問題,它的行爲不正確。 – 2013-05-05 19:47:21

+0

爲什麼你'av = new ArrayList ();'在每次迭代?把它放在'while'循環之外。 – Maroun 2013-05-05 19:51:43

+1

'這就是外觀應該看起來像什麼,但它看起來像什麼? – 2013-05-05 19:52:51

回答

1

您在閱讀等級的函數中有一個錯誤。你應該修復它以這樣的方式

// Converts String Array into Double Array 
double[] score = new double[value2.length - 1]; 
for (int i = 1; i < value2.length; i++) { 
    score[i - 1] = Double.parseDouble(value2[i]); 
} 

據我瞭解,你想要的最大值,最小值和所有等級的平均的意思。那麼你的代碼應該是這樣的:

package it.unitn.uvq.antonio.processor; 

import java.io.File; 
import java.io.FileNotFoundException;  
import java.util.ArrayList; 
import java.util.Scanner; 

public class Test { 

    public static void main(String[] args) { 
     Test x = new Test(); 
     x.high(); 
    } 

    public void high() { 
     File file2 = new File("/home/antonio/Scrivania/scores.csv"); 
     try { 
      Scanner scanner = new Scanner(file2); 
      ArrayList<Double> avgs = new ArrayList<>(); 
      double max = 0, min = 100; 
      while (scanner.hasNextLine()) { 
       String line2 = scanner.nextLine(); 
       String[] value2 = line2.split(","); 

       // Converts String Array into Double Array 
       double[] score = new double[value2.length - 1]; 
       for (int i = 1; i < value2.length; i++) { 
        score[i - 1] = Double.parseDouble(value2[i]); 
       } 

       // Finds the sum and then the average, adds it to a array List 
       double sumNum = 0.0; 
       for (double i : score) { 
        sumNum += i; 
        min = Math.min(min, i); 
        max = Math.max(max, i); 
       } 
       double avg = sumNum/10; 
       avgs.add(avg); 

       System.out.println(avg); 
      } 
      scanner.close(); 
      double avgsSum = .0; 
      for (double avg : avgs) { 
       avgsSum += avg; 
      } 
      System.out.println("High " + max + ""); 
      System.out.println("Low " + min + ""); 
      System.out.println("Average " + avgsSum/avgs.size() + ""); 
     } 
     catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
0

如果你想只平均爲每一位學生,然後最小和最大和那些總平均,你可以這樣做:

編輯:添加一個例子說明你可以如何將字符轉換爲字符。注意:我不知道你是怎麼知道它是一個B-或d +等

import java.io.File; 
import java.io.FileNotFoundException; 
import java.math.RoundingMode; 
import java.text.DecimalFormat; 
import java.util.ArrayList; 
import java.util.Scanner; 

public class Test { 

    public static void main(String[] args) { 
     Test x = new Test(); 
     x.high(); 
    } 

    public void high() { 
     // to format the scores 
     DecimalFormat df = new DecimalFormat("###.#"); 
     df.setRoundingMode(RoundingMode.HALF_UP); 

     ArrayList<Double> averages = new ArrayList<Double>(); 

     File file2 = new File("scores.csv"); 

     try { 
      Scanner scanner = new Scanner(file2); 
      while (scanner.hasNextLine()) { 
       String line = scanner.nextLine(); 
       String[] values = line.split(","); 

       // Converts String Array into Double Array 
       double[] scoresPerPerson = new double[values.length]; 

       for (int i = 1; i < values.length; i++) { 
        scoresPerPerson[i] = Double.parseDouble(values[i]); 
       } 

       // Finds the sum and then the average, adds it to a array List 
       double sum = 0.0; 

       for (double i : scoresPerPerson) { 
        sum += i; 
       } 

       double average = sum/values.length; 
       averages.add(average); 

       System.out.println(values[0] + ": " + df.format(average) + " " 
         + getCharGrade(average)); 

      } 
      scanner.close(); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 

     // Computes the average and get the max min 

     double max = 0; 
     double min = 100; 
     double totalAverage = 0; 

     for (double average : averages) { 
      if (average > max) { 
       max = average; 
      } 
      if (average < min) { 
       min = average; 
      } 
      totalAverage += average; 
     } 

     // Final step to get the general average 
     totalAverage /= averages.size(); 

     // Print out the final general data 
     System.out.println(); 
     System.out.println("High : " + df.format(max)); 
     System.out.println("Low : " + df.format(min)); 
     System.out.println("Average : " + df.format(totalAverage)); 
    } 

    private String getCharGrade(double average) { 

     if (average >= 90) { 
      return "A"; 
     } else if (average >= 80) { 
      return "B"; 
     } else if (average >= 70) { 
      return "C"; 
     } else if (average >= 60) { 
      return "D"; 
     } else { 
      return "F"; 
     } 
    } 
} 

輸出:

Alicia Marks: 82,7 B 
Bobby Ricks: 59,3 F 

High : 82,7 
Low : 59,3 
Average : 71