如何添加我通过解析获得的值以获得“列”的“值”?

时间:2015-11-20 21:54:30

标签: java loops arraylist add

我的代码通过输入文件并将其分解为“列”。

System.out.println(numberdouble);

它打印出该列中每行的值。我测试了这个以检查我的程序是否会正确地遍历列。我想知道如何输出每列的总和。

示例:

[a, 1, 2, 3]
[3, v, 3, x]
[5, 1, d, c]

如果我对第0列的值求和,我希望它将3和5加在一起。如果列中的值不是数字,则应将该值转换为0。

我的代码:

public static Double column_sum(String filename, int column)
{
    if(column < 0)
    {
        return null;
    }
    File temp = new File(filename);
    Scanner input_file;
    try
    {
        input_file = new Scanner(temp);
    }
    catch (Exception e)
    {
        return null;
    }
    ArrayList<ArrayList<String>> result = new ArrayList<ArrayList<String>>();
    while(input_file.hasNextLine())
    {
        String line = input_file.nextLine();
        List<String> myList = new ArrayList<String>(Arrays.asList(line.split(",")));
            Double numberdouble =0.0;
            String numberstring = myList.get(column);
            if(isDouble(numberstring))
            {
              numberdouble = Double.parseDouble(numberstring);
            }
            else
            {
              numberdouble=0.0;
            }
            System.out.println(numberdouble);
    }
    return 1.23; //placeholder
}

2 个答案:

答案 0 :(得分:2)

您要做的是创建一个二维矩阵。并将值存储在其中。执行此操作后,将很容易解析并添加列。 按照你的例子:

Double [][] myMatrix = new Double[3][4];
int counter = 0, rowIndex=0,columnIndex=0;
while(input_file.hasNextLine())
{   counter++;
    if(counter%4 == 0)
    {
        rowIndex++;
        columnIndex=0;
    }
    String line = input_file.nextLine();
    List<String> myList = new ArrayList<String>(Arrays.asList(line.split(",")));
        Double numberdouble =0.0;
        String numberstring = myList.get(column);
        if(isDouble(numberstring))
        {
          numberdouble = Double.parseDouble(numberstring);
        }
        else
        {
          numberdouble=0.0;
        }
        myMatrix[rowIndex][columnIndex++] = number double;
        System.out.println(numberdouble);
}

注意:索引将严格按照您的示例进行追踪。您可以使其更具可配置性。

答案 1 :(得分:1)

请参阅以下代码(我已经测试过)。由于我不了解isDouble函数的实现,因此我构建了一个简单(但不优雅)的函数。我还注意了两个未使用的行,并添加了input_file.close();来关闭资源。 &#34; test.txt&#34;的内容与输入示例完全相同:

a,1,2,3
3,v,3,x
5,1,d,c

代码:

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class ColumnSum {
    public static void main(String[] args) {
        System.out.println(column_sum("test.txt", 0));
        System.out.println(column_sum("test.txt", 1));
        System.out.println(column_sum("test.txt", 2));
        System.out.println(column_sum("test.txt", 3));
    }

    public static boolean isDouble(String s) {
        try {
            Double.parseDouble(s);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static Double column_sum(String filename, int column) {
        Double sum = 0.0;
        if (column < 0) {
            return null;
        }
        File temp = new File(filename);
        Scanner input_file;
        try {
            input_file = new Scanner(temp);
        } catch (Exception e) {
            return null;
        }
        // ArrayList<ArrayList<String>> result = new ArrayList<ArrayList<String>>();
        while (input_file.hasNextLine()) {
            String line = input_file.nextLine();
            List<String> myList = new ArrayList<String>(Arrays.asList(line.split(",")));
            Double numberdouble = 0.0;
            String numberstring = myList.get(column);
            if (isDouble(numberstring)) {
                numberdouble = Double.parseDouble(numberstring);
                sum += numberdouble;
            } else {
                numberdouble = 0.0;
            }
            // System.out.println(sum);
        }
        input_file.close();
        return sum;
    }
}

输出为(对于每列):

8.0
2.0
5.0
3.0