用递归方法求和值
sum values with recursive method
我正在尝试使用递归方法对文本文件中的值求和,文本文件中的值由分号分隔。文本文件中的数据是这样存储的:
7708190034; Environment; 10500; Schools; 8000; Health care; 9500
9609131234; Environment; 20000; Schools; 20000; Health care; 18000
假设我想对环境的值求和,这样在这种情况下输出将类似于 "Sum for environment: 30500" 等。换句话说,对于环境,我想首先阅读环境部分行(值 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
但期望的结果应该是这样的:
Sum for environment: 30500
我错过了什么或做错了什么?
根据您的格式,String[] data = s.split(";");
会得到 data.length
等于 7:
7708190034; Environment; 10500; Schools; 8000; Health care; 9500
所以它永远不会进入 if (data.length == 6)
块,并且 environmentsum
总是空的
你可以把它改成 if (data.length == 7)
查看此代码:
//Add the found number to the array:
andelarsk.add(andel);
您要添加到 andelarsk 列表(仅当您的行中有 6 列以“;”分隔时),并且要向 sum 方法传递 environmentsum
sum = sum(environmentsum, 0);
此外,每一行都会调用这个求和方法,你会得到如下内容:
Sum for environment: ..
Sum for environment: ..
Sum for environment: ..
我建议您在填充列表中的所有环境值后调用递归方法和打印语句 post while 循环。
将每行中的字段数与您要检查的数量进行比较:
if (data.length == 6) {
你可以
if (data.length > 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);
}
我正在尝试使用递归方法对文本文件中的值求和,文本文件中的值由分号分隔。文本文件中的数据是这样存储的:
7708190034; Environment; 10500; Schools; 8000; Health care; 9500
9609131234; Environment; 20000; Schools; 20000; Health care; 18000
假设我想对环境的值求和,这样在这种情况下输出将类似于 "Sum for environment: 30500" 等。换句话说,对于环境,我想首先阅读环境部分行(值 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
但期望的结果应该是这样的:
Sum for environment: 30500
我错过了什么或做错了什么?
String[] data = s.split(";");
会得到 data.length
等于 7:
7708190034; Environment; 10500; Schools; 8000; Health care; 9500
所以它永远不会进入 if (data.length == 6)
块,并且 environmentsum
总是空的
你可以把它改成 if (data.length == 7)
查看此代码:
//Add the found number to the array:
andelarsk.add(andel);
您要添加到 andelarsk 列表(仅当您的行中有 6 列以“;”分隔时),并且要向 sum 方法传递 environmentsum
sum = sum(environmentsum, 0);
此外,每一行都会调用这个求和方法,你会得到如下内容:
Sum for environment: ..
Sum for environment: ..
Sum for environment: ..
我建议您在填充列表中的所有环境值后调用递归方法和打印语句 post while 循环。
将每行中的字段数与您要检查的数量进行比较:
if (data.length == 6) {
你可以
if (data.length > 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);
}