2013-04-09 73 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)的行之后包含“尾随”或“长时程增强”开始。