在我的程序中,我使用DoubleStream
来对数组的值求和。
我有以下代码 培训P列表
import java.io.*;
import java.util.*;
import java.lang.*;
import java.util.stream.DoubleStream;
public class TrainingPList
{
int aSize=500; // size of constant
TrainingP[] arrayTrainingP= new TrainingP[aSize]; // array name + aSize constant called to define size of the array
int nextPosition=0;// size of constant
double[] MoneyList;
public void readPersonal()
{
try
{
BufferedReader br=new BufferedReader(new FileReader("TrainingP.txt"));
String theNextPublicInfo = br.readLine();
nextPosition=0;
while (theNextPublicInfo !=null)
{
String[] arrayStringPersonalInfo = theNextPublicInfo.split(",");
TrainingP tempPInfo = new TrainingP();
tempPInfo.AthleteID = arrayStringPersonalInfo[0];
tempPInfo.rMoneyRaised = Double.parseDouble(arrayStringPersonalInfo[1]);
arrayTrainingP[nextPosition] = tempPInfo;
nextPosition++;
theNextPublicInfo = br.readLine();
}
}
catch(Exception e)
{
System.out.println("Invalid read data");
}
}
public void outputarrayTrainingP()
{
for(int i=0;i<nextPosition;i++)
{
System.out.println ("Position "+":"+arrayTrainingP[i]);
}
}
public void ListAth()
{
AthList = new String[nextPosition];
for(int i=0;i<nextPosition;i++)
{
AthList[i]=arrayTrainingP[i].AthleteID+"";
}
}
public void ListMoney()
{
MoneyList = new double[nextPosition];
for(int i=0;i<nextPosition;i++)
{
MoneyList[i]=arrayTrainingP[i].rMoneyRaised;
}
DoubleStream d = DoubleStream.of(MoneyList);
double dv = d.sum();
System.out.println("The sum is " + dv);
}
public static void main(String[] args)
{
TrainingPList tp = new TrainingPList();
tp.ListMoney();
tp.readPersonal();
tp.outputarrayTrainingP();
}
}
TrainingP
public class TrainingP
{
String AthleteID;
double rMoneyRaised;
public String toString()
{
return AthleteID+","+rMoneyRaised;
}
}
文档
DJ44,136.0
DB15,0.0
rMoneyRaised
只有两个值,它们分别为136.0和0.0。但是它只返回0.0而不是值136.0。有谁知道问题是什么?
答案 0 :(得分:0)
主要方法存在问题:
public static void main(String[] args)
{
TrainingPList tp = new TrainingPList();
tp.ListMoney();
tp.readPersonal();
tp.outputarrayTrainingP();
}
您应该在阅读数据后调用tp.ListMoney()
,而不是之前。