1

我正在尝试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){
   }
}
4

1 回答 1

0

有一个if检查bill金额是否0.0,如果不是,打印它,否则,不要打印它。如果getBill()返回一个字符串,那么您需要将该字符串解析为双精度,然后在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());
    }
}
于 2013-10-31T10:48:36.900 回答