我正在尝试创建 Line 对象并将它们添加到数组列表中。我遇到的问题是排除任何彼此相似的行。我已经创建了一个比较两行以确定它们是否相等的 equals 方法。我在使用 while 循环时遇到问题。我没有错误消息。它编译得很好。它只是不会从文本文件中读取。我被困住了,不知道从这里还能去哪里。
public void read( File fileName ) throws Exception
{
reader = new Scanner(fileName);
//---------------------
//Have to read the first number before starting the loop
int numLines = reader.nextInt();
lines = new ArrayList <Line> (numLines);
//This loop adds a new Line object to the lines array for every line in the file read.
while( reader.hasNext() ) {
for( int i = 0; i < numLines; i++ ) {
int x = reader.nextInt();
int y = reader.nextInt();
Point beg = new Point(x,y);
x = reader.nextInt();
y = reader.nextInt();
Point end = new Point(x,y);
String color = reader.next();
Line l = new Line( beg, end, color );
if (l.equals(lines.get(i)))
break;
else
lines.add(i, l);
}
}
//Print the action to the console
System.out.println( "reading text file: " + fileName );
reader.close();
}