1

I'm trying to sum values from a text file with a recursive method, the values in the textfile are separated by a semicolon. The data in the textfile is stored like this:

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

Let's say I want to sum the values for environment so that in this case the output would be something like "Sum for environment: 30500" etc. In other words, for environment I want to read the part for environment in the first line (value 10500) and then read the next line and add the found value (in this case 20000) to the first one, and finally print out the sum for it. And the summation should be done with a recursive method. At the moment I use the following code:

/*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;
}

At the moment I only get an output like this:

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

But the DESIRED result should be like this:

Sum for environment: 30500

What am I missing or doing wrong?

2
  • Hint: where do you add something in the list? Commented Feb 27, 2015 at 16:55
  • Within the if-statement in the readEnvironment-method I add values to the array "environmentsum" with the line environmentsum.add(number); Commented Feb 27, 2015 at 16:59

4 Answers 4

3

String[] data = s.split(";"); would get a data.length equals to 7 according to your format:

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

so it never enters if (data.length == 6) block, and environmentsum is always empty

You could just change it to if (data.length == 7)

Sign up to request clarification or add additional context in comments.

6 Comments

No, since every part separated by the semicolon represents a position in an array, and an array starts from index 0. In other words the value for environment is on index 2 (0,1,2).
@equinna I didn't say the array doesn't start from index 0. Try print out data.length, it should be 7. data[2] is "10500", data[5] is " Health care", data[6] is "9500"
Just like you say, data[2] is "10500", and as you can see I'm using the line "number = Integer.parseInt(data[2]);" to specify that it's that value (position) I want to use in the calculation from the first row. So the if-statement should still be data.length == 6. I changed it to "== 7" just in case, but just like I thought it still doesn't work, that's not the problem.
@eqinna how many items in environmentsum when you pass it to sum() function?
In other words, it should not be "data.length == 7" since I've not put something in data[7] - I only use data[0] - data[6] = length 6. :-)
|
2

Compare the number of fields in each row against the amount that you're checking against:

if (data.length == 6) {

You could do

if (data.length > 2) {

1 Comment

Could you give a more specific code sample, please?
0

See this code:

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

You are adding to andelarsk list (only if you have 6 columns in your line seperated by ";") and to sum method you are passing environmentsum

sum = sum(environmentsum, 0);

Also this sum method will be called for each and every line and you will get something like:

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

I would suggest you call your recursive method and print statement post while loop once you have populated all environment values in the list.

Comments

-2

I solved it myself with the following code:

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.