类对象未显示导入文件中的正确信息;找不到数据丢失的地方

时间:2018-09-14 16:02:25

标签: java

除导入的数据外,其他所有内容均正常运行;这些数字根本不会被读取(除了0)。问题可能出在BufferedReader(我不经常使用的东西)上,或者可能是我的数组列表组织出错。没有错误,所以我不确定数据丢失的地方。

显示信息的类:

  import java.io.*;
  import java.util.*;
  import javax.swing.*;
  import java.text.*;    

    public class Sales
    {
       public static void main(String[] args) throws IOException
       {
          final int ONE_WEEK = 7;
          double[] sales = new double[ONE_WEEK];

          SalesData week = new SalesData(sales);

          DecimalFormat dollar = new DecimalFormat("#,##0.00");

          JOptionPane.showMessageDialog(null,
                    "The total sales were $" +
                    dollar.format(week.getTotal()) +
                    "\nThe average sales were $" +
                    dollar.format(week.getAverage()) +
                    "\nThe highest sales were $" +
                    dollar.format(week.getHighest()) +
                    "\nThe lowest sales were $" +
                    dollar.format(week.getLowest()));
          System.exit(0);
       }

    }

收集和整理信息的类:

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

  public class SalesData{
     private double[] sales;

     public SalesData(double[] s) throws IOException{
        sales = new double[s.length];

        for (int index = 0; index < s.length; index++){
           sales[index] = s[index];
        }

           File f = new File("SalesData.txt");

           BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
           String str = input.readLine();
           String[] stringSales = str.split(",");
           Double[] doubleSales = new Double[str.length()];

        for (int i=0; i<stringSales.length;i++){
           doubleSales[i] = Double.parseDouble(stringSales[i]);
           }
        }

     public double getTotal(){
        double total = 0.0;

        for (int index = 0; index < sales.length; index++){
           total += sales[index];
        }
        return total;
     }

     public double getAverage(){
        return getTotal() / sales.length;
     }

     public double getHighest(){
        double highest = sales[0];

        for (int index = 1; index < sales.length; index++){
           if (sales[index] > highest)
           highest = sales[index];
           }

        return highest;
        }

     public double getLowest(){
        double lowest = sales[0];
        for (int index = 1; index < sales.length; index++){
           if (sales[index] < lowest)
              lowest = sales[index];
           }
        return lowest;
     }
  }

归档以下文件:

1245.67,1490.07,1679.87,2371.46,1783.92,1461.99,2059.77
2541.36,2965.88,1965.32,1845.23,7021.11,9652.74,1469.36
2513.45,1963.22,1568.35,1966.35,1893.25,1025.36,1128.36

1 个答案:

答案 0 :(得分:1)

至少一个问题在这里

String[] stringSales = str.split(",");
Double[] doubleSales = new Double[str.length()];// this will be 55 ish instead of 7

您想要Double[] doubleSales = new Double[stringSales.length()];

相关问题