用递归方法求和值

时间:2015-02-27 16:52:28

标签: java recursion sum

我正在尝试使用递归方法对文本文件中的值求和,文本文件中的值由分号分隔。文本文件中的数据存储如下:

7708190034; Environment; 10500; Schools; 8000; Health care; 9500
9609131234; Environment; 20000; Schools; 20000; Health care; 18000

假设我想要对环境的值求和,以便在这种情况下输出类似于“环境总和:30500”等等。换句话说,对于环境我想在第一个环境中读取环境的部分line(值10500)然后读取下一行并将找到的值(在本例中为20000)添加到第一行,最后打印出它的总和。并且应该使用递归方法完成求和。目前我使用以下代码:

/*Main method:*/
public static void main(String[] args) throws IOException {
    //Invoke method to sum environments through recursive method:
    readEnvironment();
}

/*Method to read environment values:*/
static void readEnvironment() throws IOException {
    List<Integer> environmentsum = new ArrayList<>();
    File file = new File("sums.txt");

    FileReader fr = new FileReader(file);
    BufferedReader br = new BufferedReader(fr);

    // Read the first line from the file
    String s = br.readLine();
    // As long as we have not reached the end of the file...
    while (s != null) {
        // Split the string read from the file by the separator ;
        String[] data = s.split(";");

        //As long as the string has 6 parts...
        if (data.length == 6) {
            // We know that the second part in the line is the value for an environment:
            int number = Integer.parseInt(data[2]);
            //Add the found number to the array:
            environmentsum.add(number);
        }

        int sum = 0;
        //Invoke recursive method:
        sum = sum(environmentsum, 0);
        System.out.println("Sum for environment: " + sum);
        //Read next line:
        s = br.readLine();
    }
    br.close();
}

/*Recursive method to sum the values:*/
static int sum(List<Integer> t, int index) {
    //If there are no elements in the array:
    if (index == t.size()) {
        return 0;
    }
    int sum = t.get(index) + sum(t, index + 1);
    return sum;
}

目前我只得到这样的输出:

Sum for environment: 0
Sum for environment: 0
Sum for environment: 0

但是DESIRED的结果应该是这样的:

Sum for environment: 30500

我错过了什么或做错了什么?

4 个答案:

答案 0 :(得分:3)

根据您的格式,

String[] data = s.split(";");会得到data.length等于 7

7708190034; Environment; 10500; Schools; 8000; Health care; 9500 

因此它永远不会输入if (data.length == 6)块,而environmentsum始终为空

您可以将其更改为(data.length == 7)

答案 1 :(得分:2)

将每行中的字段数量与您要检查的金额进行比较:

if (data.length == 6) {

你可以做到

if (data.length > 2) {

答案 2 :(得分:0)

请参阅此代码:

//Add the found number to the array:
andelarsk.add(andel);

您要添加到andelarsk列表(仅当您的行中有6列由&#34;;&#34;分隔时)和sum方法通过环境

sum = sum(environmentsum, 0);

此外,还会为每一行调用此sum方法,您将得到类似的内容:

Sum for environment: ..
Sum for environment: ..
Sum for environment: ..

我建议你在填充列表中的所有环境值后调用你的递归方法并在循环时打印语句。

答案 3 :(得分:-2)

我用以下代码解决了这个问题:

public static void readEnvironment() throws IOException {

    List<Integer> all = new ArrayList<>();
    File file = new File("test.txt");
    FileReader fr = new FileReader(file);
    BufferedReader br = new BufferedReader(fr);
    String s = br.readLine();

    while (s != null) {
        String[] data = s.split(";");
        //We know that the second part in a line is the amount for environment:
        int a = Integer.parseInt(data[2]);
        s = br.readLine();
        all.add(a);
    }
    br.close();

    int sum = 0;
    //Invoke the sum method:
    sum = sum(all, 0);
    System.out.println("Sum for environment: " + sum);
}