从文本文件中删除具有特定元素的行

时间:2013-10-31 10:45:06

标签: java

我正在尝试从文本文件中删除0.0的所有行。

这是它输出的内容:

0037823478362839 0.0
0236530128715607 3.88
0425603748320896 36.09
0659644925904600 13.58
0823485731970306 0.0
0836430488858603 46.959999999999994

这就是我想要输出的内容

0236530128715607 3.88
0425603748320896 36.09
0659644925904600 13.58
0836430488858603 46.959999999999994

代码:

// Collects the billing information and outputs them to a user defined .txt file
public void getBill() {
   try {
        PrintStream printStream = new PrintStream(outputFile);
        Passenger[] p = getAllPassengers();
        for(Passenger a : p){
            printStream.print(a.getCardNumaber() + " ");
            printStream.println(a.getBill());
        }
        printStream.close();
   } catch(Exception e){
   }
}

1 个答案:

答案 0 :(得分:0)

if检查bill金额是否为0.0,如果不是,请打印,否则请勿打印。如果getBill()返回一个String,那么你需要将该String解析为double,然后在if中检查它。

for(Passenger a : p){
    if(a.getBill() != 0.0){ // the if to check the value of bill
        printStream.print(a.getCardNumaber() + " ");
        printStream.println(a.getBill());
    }
}

for(Passenger a : p){
    double dBill = Double.parseDouble(a.getBill()); // in case getBill() returns a String
    if(dBill != 0.0){ // the if to check the value of bill
        printStream.print(a.getCardNumaber() + " ");
        printStream.println(a.getBill());
    }
}
相关问题