Java 缓冲读取器不读取某些行
这是我的节目Java 缓冲读取器不读取某些行,java,bufferedreader,Java,Bufferedreader,这是我的节目 String thisSchool=buffer.readLine(); String thisLine; while((thisLine = buffer.readLine()) != null){ if(thisLine=="*") { thisSchool=buffer.readLine(); } e
String thisSchool=buffer.readLine();
String thisLine;
while((thisLine = buffer.readLine()) != null){
if(thisLine=="*")
{
thisSchool=buffer.readLine();
}
else
{
School school = new School(thisSchool);
Student student = new Student(thisLine, school);
StudentList.add(student);
}
}
我的文本文件如下所示:
School1
A
B
C
*
School2
D
E
*
School3
F
A School1
B School1
C School1
* School1
School2 School1
D School1
E School1
* School1
School3 School1
F School1
我的驱动程序类的输出是:
School1
A
B
C
*
School2
D
E
*
School3
F
A School1
B School1
C School1
* School1
School2 School1
D School1
E School1
* School1
School3 School1
F School1
这就是我想要的样子
A School1
B School1
C School1
D School2
E School2
F School3
以下是问题的症结所在
“currentSchool”变量从未改变过,我也不知道为什么!“*”被视为一个学生(我想用它作为分隔符,即程序在遇到它时会忽略它)。相反,“if line=*”命令被完全忽略,因此,学校从未改变,学生的书写也不正确
if(thisLine=="*")
不要使用“==”来比较对象
使用equals()
方法:
if(thisLine.equals("*"))
哇,我现在感觉很糟糕…:)谢谢用于比较,但在本例中不适用如果要确定两个基本体是否相同,或两个对象是否为同一对象,则使用。在本例中,它不起作用,因为thisLine和“*”来自不同的来源,所以它们永远不会是==即使它们是相同的。这就是为什么你使用。等于(…)是的,我知道使用==。。。我只是太专注于另一个语法错误而没有注意到我的错误