【问题标题】:I/O File Exception: ArrayList Values w/o including the first two elements (JAVA)I/O 文件异常:不包含前两个元素的 ArrayList 值 (JAVA)
【发布时间】:2016-07-29 15:52:33
【问题描述】:

我需要知道如何在不使用来自 I/O 文件异常的 ArrayList 的前两个值的情况下形成总和方程。 我的总和不应该包括前两个元素,即权重 0.5 和最小数字 3。所有值都是:0.5、3、10、70、90、80、20。这些数字来自输入文件,“数据.txt”。另外,我需要做出 try-with-resources 声明。我是新手,刚刚学过,但我想知道如何将它应用到我自己的程序中。

public class CalcWeightedAvgDropLowest {

public static void main(String[] args) throws FileNotFoundException {

       ArrayList<Double> inputValues = getData();
       double weightedAvg = calcWeightedAvg(inputValues);
       printResults(inputValues, weightedAvg);

}

public static ArrayList<Double> getData() throws FileNotFoundException {       

        // Prompts for the input file names                     

        Scanner in = new Scanner(new File("data.txt"));
        ArrayList<Double> inputValues = new ArrayList<Double>(); 

        while (in.hasNextDouble())
            {
                inputValues.add(in.nextDouble());                    
            }         

        in.close();


        return inputValues;
}

public static double calcWeightedAvg(ArrayList<Double> inputValues) throws FileNotFoundException {
    // calc weighted av          
     double sum = 0;
     double average = 0;                
     int i = 0;
     double weightavg = 0;


            // Calcuates the average of the array list with the lowest numbers dropped
            // calculated average is 42.5
            for (i = 0; i < inputValues.size(); i++) 
            {                    
                if (inputValues.get(i) > inputValues.get(1)) 
                { 
                   // **I just need an equation for the sum here w/o the first two values.**
                }
            }
            average = sum /inputValues.size();

            weightavg = average * inputValues.get(0);


            return weightavg;
}

public static void printResults(ArrayList<Double> inputValues, double weightedAvg) throws FileNotFoundException {
        Scanner scnr = new Scanner(System.in);
        System.out.print("Output File: ");
        String outputFileName = scnr.next();
        PrintWriter out = new PrintWriter(outputFileName);
            out.print("The weighted average of the numbers is " + weightedAvg + ", when using the data " + inputValues + ", where " +inputValues.get(0)+ " is the weight used, and the average is computed after dropping the lowest " +inputValues.get(1)+ " values.");
            out.close();
    }
}

【问题讨论】:

  • 当你说“前两个”时,这些元素是 0 和 1,还是它们是最小的两个元素?
  • @AndyTurner 前两个元素,0 和 1。
  • 你可以从位置 2 而不是 0 开始
  • @LuizAgner 你是不是这个意思for (i = 2; i &lt; inputValues.size(); i++)
  • @OliveBassey 没错。然后它将从 inputValues 数组的第三个位置(索引 2)开始,忽略前两个 Double 数字。

标签: java arraylist average ioexception try-with-resources


【解决方案1】:

您可以使用以下方法查看从 2 到结尾的列表元素:

List<Double> view = inputValues.subList(2, inputValues.size());

(假设您在列表中至少有 2 个元素)。

然后将calcWeightedAverage的参数改成List&lt;Double&gt;,改为传递view

calcWeightedAvg(view);

【讨论】:

  • 这将如何工作?我需要 calcWeightAvg 的参数 inputValues 才能从 getData() 方法中找到 arraylist 元素。
  • 您说您需要跳过前 2 个元素。这会跳过前两个元素。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-17
  • 2016-11-26
  • 1970-01-01
  • 2012-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多