2015-02-11 10 views
0

在我的高级java类中,我们将编写一个应用程序,该应用程序读取由管道(“|”)分隔的.txt文件(“nasdaqlisted.txt”),并提取所有具有测试问题的股票= “Y”。我的应用程序读取我的文件,但仍然打印出测试用例。我试图使用if语句来比较testIssue中存储的内容为“Y”,但我无法弄清楚为什么这不起作用。下面是我的源代码:为什么在Java中没有“!sort.getTestIssue()。equals(”Y“)”从这个nasdaqlisted.txt中删除测试股票?

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.Scanner; 
import java.util.NoSuchElementException; 
import java.util.StringTokenizer; 

public class readTextFile 
{ 
    private Scanner input = null; 
    private File file = null; 

    public void openFile() 
    { 
     try 
     { 
      file = new File("nasdaqlisted.txt"); 
      input = new Scanner(file); 
     } 
     catch (FileNotFoundException fileNotFoundException) 
     { 
      System.err.println("Error opening file."); 
      System.exit(1); 
     } 
    } 

    public void readFile() 
    { 
     sortTextFile sort = new sortTextFile(); 
     try 
     { 
      try 
      { 
       file = new File("nasdaqlisted.txt"); 
       input = new Scanner(file); 
      } 
      catch (FileNotFoundException fileNotFoundException) 
      { 
       System.err.println("Error opening file."); 
       System.exit(1); 
      } 


      while (input.hasNext()) 
      { 
       StringTokenizer st = new StringTokenizer(input.nextLine(), "|"); 
       while (st.hasMoreTokens()) 
       { 
        input.nextLine(); 
        sort.setSymbol(st.nextToken()); 
        sort.setSecurity(st.nextToken()); 
        sort.setMarket(st.nextToken()); 
        sort.setTest(st.nextToken()); 
        sort.setFinancial(st.nextToken()); 
        sort.setSize(st.nextToken()); 

        if (!sort.getTestIssue().equals("Y")) 
        { 
         System.out.println(sort.getSymbol()); 
         System.out.println(sort.getSecurityName()); 
         System.out.println(sort.getTestIssue()); 
        } 
       } 
      } 
     } 
     catch (NoSuchElementException noSuchElementException) 
     { 
      System.err.println("Improperly formed file."); 
      System.exit(1); 
     } 

    } 
    public void closeFile() 
    { 
     if (input != null) 
     { 
      input.close(); 
     } 
    } 
} 

这里是我与测试类股获得输出的末尾还打印出:

Security Name: Zalicus Inc. - Common Stock 
Test Issue: N 
Symbol: ZN 
Security Name: Zion Oil & Gas Inc - Common Stock 
Test Issue: N 
Symbol: ZOLT 
Security Name: Zoltek Companies, Inc. - Common Stock 
Test Issue: N 
Symbol: ZU 
Security Name: zulily, inc. - Class A Common Stock 
Test Issue: N 
Symbol: ZVZZT 
Security Name: NASDAQ TEST STOCK 
Test Issue: Y 
Symbol: ZXYZ.A 
Security Name: Nasdaq Symbology Test Common Stock 
Test Issue: Y 
Improperly formed file. 

Process finished with exit code 1 
+1

使用调试器,并检查sort.getTestIssue的值()。它可能包含一个白色空间。 – 2015-02-11 20:24:20

回答

1

如果System.out.println(sort.getTestIssue());输出“测试问题:Y”,那么sort.getTestIssue()不等于“Y”。

也许你想使用endsWith或正则表达式或别的东西

+0

好的,谢谢,我会试试看看它是否修复。 – 2015-02-11 20:45:25

+0

啊,它工作!现在,我的输出中间出现NoSuchElementException错误。 – 2015-02-11 20:50:17

相关问题