2013-04-09 40 views
1

這是我的程序。它不應該打印以單詞「LTP」開頭的句子或含有單詞「TRAILING」的句子,但它不會這樣做。我不明白爲什麼。請幫助我。字符串功能不能正常工作

import java.io.*; 

public class FileInputDemo { 
    public static void main(String args[]) { 
     // args.length is equivalent to argc in C 
     try { 
      // Open the file that is the first 
      // command line parameter 
      FileInputStream fstream = new FileInputStream(
        "C:\\Documents and Settings\\work\\Desktop\\New Folder\\New Folder\\02Apr2013log1.txt"); 
      // Convert our input stream to a 
      // DataInputStream 
      DataInputStream in = new DataInputStream(fstream); 
      // Continue to read lines while 
      // there are still some left to read 
      while (in.available() != 0) { 
       String s=in.readLine(); 
       // Print file line to screen 
      if(s.startsWith("LTP") || s.contains("TRAILING")) 
      { 
       continue; 
      } 

      System.out.println(in.readLine()); 
      } 

      in.close(); 
     } catch (Exception e) { 
      System.err.println("File input error"); 
     } 
    } 
} 

這是輸出

Long trade Managers in list (from Limit removal process) 2 
LTP 9708.0 Current Stop 9723.0 for Order ID BAA0001 Mode:- TRAILING 
9711.0 9716.0 9707.0 9707.0 9710.62 
BullishFactor [openBullishFactor=NEUTRAL, closeBullishfactor=BEARISH, closeToOpenFactor=MODERATE_BEARISH] 
BarSharing [sharingType=LLLH, bodySharing=0.44] 
LTP 9707.0 Current Stop 9717.0 for Order ID BAA0001 Mode:- TRAILING 
+0

請不要使用DataInputStream來讀取文本,也請將它從您的示例中刪除,並且經常複製此不良想法。 http://vanillajava.blogspot.co.uk/2012/08/java-memes-which-refuse-to-die.html – 2013-06-24 07:08:54

回答

0

不要再從System.out.println(in.readLine());的行中讀取。它將讀取下一行並且不檢查LTP | TRAILING可以在線上找到。將其替換爲System.out.println(s);

+1

OP的措辭有點令人困惑,但說*它**不應該**打印以字開頭的句子「LTP」或包含單詞「TRAILING」的句子* – 2013-04-09 05:52:29

+0

@JonLin謝謝。在瀏覽帖子時,我明白了相反的意思。將編輯帖子。我需要更多的咖啡:( – 2013-04-09 05:53:19

+0

謝謝..解決了這個問題..我不喜歡打印System.out.println(s)而不是System.out.println(in.readLine())。 – maddy 2013-04-09 05:56:46

3

不應該被System.out.println打印字符串s

while (in.available() != 0) { 
    String s=in.readLine(); 

    if(s.startsWith("LTP") || s.contains("TRAILING")) 
    { 
     continue; 
    } 

    System.out.println(s); 
} 

如果打印出來in.readLine()它可能你剛纔讀的一個(存儲在s)的行之後包含「尾隨」或「長時程增強」開始。