Java 通过循环生成数组

Java 通过循环生成数组,java,Java,这是我的代码,我需要帮助了解如何正确标记每个标记,并在循环的每个周期将它们放在一个数组中,以及如何获得数组的和,以及如何获得最远的距离值 import java.io.*; import java.util.*; public class Data{ public static void main ( String[] args ) throws IOException{ String Filename = "Data.txt" ; String line;

这是我的代码,我需要帮助了解如何正确标记每个标记,并在循环的每个周期将它们放在一个数组中,以及如何获得数组的和,以及如何获得最远的距离值

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

public class Data{ 
public static void main ( String[] args ) throws IOException{ 
  String Filename = "Data.txt" ; 
    String line;

      FileReader Filereader = new FileReader(Filename);
      BufferedReader input = new BufferedReader(Filereader);
      line = input.readLine(); 

      System.out.println("--- oOo ---");
      System.out.println("AVERAGE ACID LEVEL");
      System.out.println("--------------------------------------------");

        double[] nums = new double[13];
        int sum = 0;

      while ( line != null ) // continue until end of file 
      { 

        StringTokenizer token = new StringTokenizer(line);



            for ( int i = 0; i < nums.length; i++ )
           {

              String temp = input.readLine();
              nums[i] = Double.parseDouble(temp);

              System.out.println(nums[i]);
           }

      } 
      input.close(); 

} 
    }
任何帮助都将不胜感激。。。
谢谢

因为您的数据值每次都在一个新行上,所以您不需要
StringTokenizer
,因为您可以从该行读取值

您也不需要在
while
循环中有嵌套的
for
循环,while循环会读取每一行,因此基本上在while循环中执行此操作

  • 读取值
  • 添加到数组(使用so可以具有动态长度)
  • 相加
  • 比较一下是否最远
  • 试试这个

    while ((line = input.readLine()) != null)
    
    而不是

    line = input.readLine(); // it having the first value
    
    因为,您必须逐行读取文件

    while ( line != null ) // so only your loop is unbreakable
    
    不要复制和粘贴。试着去理解

    while ((line = input.readLine()) != null) // This will read the file line by line till last value.
            {
                values[i] = Double.valueOf(line); 
                i++;                          // This is for finding the total number of values from the file.
            }
    
            Double sampleInput = 0.0;
            for(Double valueArray : values)
            {
                sampleInput = sampleInput + valueArray; // Atlast we sum all the array values.
            }
    
            Double output = (double) sampleInput/values.length;
    

    如果您正在问这些问题,那么您一定没有编写此代码。我们不是来帮你做家庭作业的。不要只是粘贴代码并说“添加这个和那个”。我仍然不知道为什么家庭作业标签从stackoverflow中删除。+1-这是(IMO)针对此类问题的正确答案。OP学会自己编程是很重要的!!
    while ((line = input.readLine()) != null) // This will read the file line by line till last value.
            {
                values[i] = Double.valueOf(line); 
                i++;                          // This is for finding the total number of values from the file.
            }
    
            Double sampleInput = 0.0;
            for(Double valueArray : values)
            {
                sampleInput = sampleInput + valueArray; // Atlast we sum all the array values.
            }
    
            Double output = (double) sampleInput/values.length;