Reputation: 321
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?
Upvotes: 1
Views: 552
Reputation: 321
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);
}
Upvotes: -2
Reputation: 159754
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) {
Upvotes: 2
Reputation: 6138
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)
Upvotes: 3
Reputation: 37023
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.
Upvotes: 0