gpt4 book ai didi

java - 为什么在 Java 中 "!sort.getTestIssue().equals("Y")"不从 nasdaqlisted.txt 中删除测试股票?

转载 作者:行者123 更新时间:2023-12-01 11:55:38 25 4
gpt4 key购买 nike

在我的高级 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());输出“测试问题:Y”,则sort.getTestIssue()不等于"is"。

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

关于java - 为什么在 Java 中 "!sort.getTestIssue().equals("Y")"不从 nasdaqlisted.txt 中删除测试股票?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28463744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com