【问题标题】:Finding the averages of a text file using arrays使用数组查找文本文件的平均值
【发布时间】:2015-11-06 17:44:36
【问题描述】:

我有一个包含 922 行和 11 列的文本文件。我需要将它们按列分开,然后在每列中找到所有 922 个数字的平均值。我是初学者,所以请耐心等待。我在正确地将数字放入数组方面最努力,第一行是单词,所以我不知道如何处理它们。这是我到目前为止所拥有的。任何有关将数据存储为 int 类型的帮助都会有所帮助。谢谢。

     public static void main(String[] args) 
     throws FileNotFoundException, IOException {
            try (BufferedReader br = new BufferedReader(new FileReader("eeg.txt")))
                    { 
                    int[] sCurrentLine;
                    while ((br.readLine()) != null) {


                        int[] information = ;
                        int time = information[0];
                        int attention = information[1];
                        int med = information[2];
                        int delta = information[3];
                        int theta = information[4];
                        int lA = information[5];
                        int hA = information[6];
                        int lB = information[7];
                        int hB = information[8];
                        int lG = information[9];
                        int hG = information[10];

}

}
 }

【问题讨论】:

    标签: java arrays average


    【解决方案1】:

    你必须把你读到的那行赋值给一个字符串,然后把这个字符串解析成一个整数数组:

    String someLine = "";
    while ((someLine = br.readLine()) != null) {
        String strArray [] = someLine.split();
        int[] information = new int[strArray.length];
        for (int i=0;i<strArray.length;i++){
            information[i] = Integer.parse(strArray[i]);
        }
        // process the information array
    }
    

    【讨论】:

      猜你喜欢
      • 2017-06-30
      • 1970-01-01
      • 2017-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多