在java中按列总结列

时间:2014-11-09 07:45:28

标签: java arrays io

i have file txt in desktop :
1   5     23
2   5     25  
3   30    36

i want sum column by column 1 + 2 + 3 =... and 5 + 5...n and 23,...n

Scanner sc = new Scanner (file("patch");
while (sc.hasNextLine)
  

{

     

//每列的总和

     

}

     

请帮助我,谢谢

3 个答案:

答案 0 :(得分:2)

我会使用try-with-resources使用Scanner来清理File。此外,您可以围绕Scanner输入构建line以获取int列(不需要关闭,因为String(s)不是#39; t无论如何都可以关闭)。像,

try (Scanner sc = new Scanner(new File("patch"))) {
    while (sc.hasNextLine()) {
        String line = sc.nextLine();
        Scanner row = new Scanner(line);
        long sum = 0;
        int count = 0;
        while (row.hasNextInt()) {
            int val = row.nextInt();
            if (count == 0) {
                System.out.print(val);
            } else {
                System.out.printf(" + %d", val);
            }
            sum += val;
            count++;
        }
        System.out.println(" = " + sum);
    }
} catch (IOException e) {
    e.printStackTrace();
}

作为Scanner(String)构造函数Javadoc文档

  

构造一个新的Scanner,用于生成从指定字符串扫描的值。

编辑要对列进行求和有点棘手,但您可以将所有内容读入多维List<List<Integer>>之类

try (Scanner sc = new Scanner(new File("patch"))) {
    List<List<Integer>> rows = new ArrayList<>();
    int colCount = 0;
    while (sc.hasNextLine()) {
        List<Integer> al = new ArrayList<>();
        String line = sc.nextLine();
        Scanner row = new Scanner(line);
        colCount = 0;
        while (row.hasNextInt()) {
            colCount++;
            int val = row.nextInt();
            al.add(val);
        }
        rows.add(al);
    }
    for (int i = 0; i < colCount; i++) {
        long sum = 0;
        for (List<Integer> row : rows) {
            sum += row.get(i);
        }
        if (i != 0) {
            System.out.print("\t");
        }
        System.out.print(sum);
    }
    System.out.println();
} catch (IOException e) {
    e.printStackTrace();
}

编辑2 为了提高效率,您可能更愿意使用Map之类的

try (Scanner sc = new Scanner(new File("patch"))) {
    Map<Integer, Integer> cols = new HashMap<>();
    while (sc.hasNextLine()) {
        String line = sc.nextLine();
        Scanner row = new Scanner(line);
        int colCount = 0;
        while (row.hasNextInt()) {
            int val = row.nextInt();
            if (cols.containsKey(colCount)) {
                val += cols.get(colCount);
            }
            cols.put(colCount, val);
            colCount++;
        }
    }
    for (int i : cols.values()) {
        System.out.printf("%d\t", i);
    }
    System.out.println();
} catch (IOException e) {
    e.printStackTrace();
}

答案 1 :(得分:1)

请找到代码。请仔细阅读评论。 这是您参考的一种方式。我希望您尝试其他方法来提高您的知识,而不仅仅是使用此代码。

int sums[] = null;
    while (sc.hasNextLine())
    {
        String row = sc.next();// get first row
        String[] values = row.split(" ");// split by space
        if(null == sums)
        {
            sums = new int[values.length];// create sum array with first row size
        }
        int index = 0;
        for (String value : values)
        {
            sums[index] = sums[index]+Integer.parseInt(value);//adding current row value to current sum
            index++;
        }
    }
    if(null != sums)
    {
        int index=0;
        for (int sum : sums)
        {
            System.out.println("Sum of column "+index+" : "+sum);// Printing each column sum
            index++;
        }
    }

答案 2 :(得分:0)

如果您的文件是CSV格式,则用逗号分隔(&#34;,&#34;)并根据拆分数组长度查找列数。

如下所示:

String line = sc.next();
String[] lineArr = line.split(",");
int len = lineArr.length;

创建大小为len的arraylists数组,并将每个列字段存储在相应的arraylist中。 最后,最后在每个arraylist上应用sum来计算每个列值的总和。