在我的高级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
使用调试器,并检查sort.getTestIssue的值()。它可能包含一个白色空间。 – 2015-02-11 20:24:20