为什么在 Java 中没有 "!sort.getTestIssue().equals("Y")" 从这个 nasdaqlisted.txt 中删除测试股票?

Why in Java doesn't "!sort.getTestIssue().equals("Y")" get rid of the test stocks from this nasdaqlisted.txt?

在我的高级 java class 中,我们将编写一个应用程序来读取由管道 ("|") 分隔的 .txt 文件 ("nasdaqlisted.txt") 并拉出所有有测试问题的股票 = "Y"。我的应用程序读取我的文件,但仍然打印出测试用例。我正在尝试使用 if 语句将 testIssue 中存储的内容与 "Y" 进行比较,但我不明白为什么这不起作用。这是我的源代码:

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

如果System.out.println(sort.getTestIssue());输出"Test Issue: Y",则sort.getTestIssue()不等于"Y"。

也许您想使用 endsWith 或正则表达式或其他东西