java在读取带有列表的文件时获取所有空值
嗨,读取文件有问题。我的文件内容是:
Alcohol 0,000425 0,000385 0,00003 0,89.
它表明所有变量都等于null。 我在这段代码中找不到错误。也许过滤器或foreach循环有问题
public class FileReading {
List<Double> Mass1 = new ArrayList<>();
List<Double> Mass2 = new ArrayList<>();
List <Double> Voulume = new ArrayList<>();
List<Double> Height = new ArrayList<>();
void Reading(String name,int sk) throws IOException{
List<String> Material= new ArrayList<>();
Material = Files.lines(Paths.get("D:/Desktop/NewFolder/26laboratorinis.txt")).
filter(s->s.contains(name))
.map(s ->s.substring(sk)).collect(Collectors.toList());
for(String number : Material){
String[] l = number.split(" ");
Mass1.add(Double.parseDouble(l[0]));
Mass2.add(Double.parseDouble(l[1]));
Volume.add(Double.parseDouble(l[2]));
Height.add(Double.parseDouble(l[3]));
}
}
public void setMass1(List<Double> PAM) {
this.Mass1 = PAM;
}
public void setMass2(List<Double> TAM) {
this.Mass2 = TAM;
}
public void setVolume(List<Double> T) {
this.Volume =T;
}
public void setHeight(List<Double> VSA) {
this.Height = VSA;
}
public List<Double> getMass1() {
return Mass1;
}
public List<Double> getMass2() {
return Mass2;
}
public List<Double> getVolume() {
return Volume;
}
public List<Double> getHeight() {
return Height;
}
}
# 1 楼答案
调用Reading方法时,您会将哪些值传递给它
不管怎样:
Files.lines
-以流的形式读取文件中的所有行。 然后将每一行放入filter
方法中。所以,如果这一行不包含您的参数name,它只会“扔掉”这一行。在该方法之后
map
将所有过滤行转换为s.substring(sk)
。最后
collect
方法将流转换为列表所以,可能您传递的名称在文档的任何一行中都不包含内容,这就是为什么您得到null