2013-10-31 59 views
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){ 
    } 
} 
+0

向我們展示'Passenger'的代碼,特別是'getBill()'的返回類型。 – 2013-10-31 10:47:51

+1

對於初學者:它被稱爲Java,而不是JAVA,它不是首字母縮寫詞。 – Bobby

+0

看來你只需要測試a.getBill()的值。如果它等於0,那麼你不打印相應的行。 – Julien

回答

0

有一個if來檢查bill量是0.0如果不是,打印它,否則,不要公關int it。如果getBill()返回一個字符串,則需要將該字符串解析爲一個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()); 
    } 
} 
+0

謝謝,作品魅力:D +1 – user2940972

+0

@ user2940972 - 很高興聽到! :)請[接受](http://meta.stackexchange.com/a/5235/216721)答案,以防止它幫助您解決問題。 – SudoRahul

+0

剛讓我去:)謝謝:D – user2940972