Java equals有关问题
Java equals问题
System.out.println("Please chose the Student you want to delete");
Scanner scanner = new Scanner(System.in);
String s = scanner.next();
Iterator<Student> iterator = set.iterator();
if (iterator.hasNext()) {
Student type = (Student) iterator.next();
// System.out.println(s .equals(type.num));
// System.out.println(s.length());
System.out.println(type.num.length());
if (s .equals(type.num)) {
//System.out.println(s);
//System.out.println(type.num);
set.remove(type);
new Append().add(set);
System.out.println(type);
System.out.println("Has deletede it");
}else {
System.out.println("No this one");
}
}
我这代码.输入的和原有的 Student.num 是相同的,但是 有时候可以删除,有时候不能删除,有时候长度相同,有时候长度不同.为什么?
怎么办?
------解决方案--------------------
if (iterator.hasNext()) {
换为 while(iterator.hasNext())
if 只走一次,while会一直循环下去的
System.out.println("Please chose the Student you want to delete");
Scanner scanner = new Scanner(System.in);
String s = scanner.next();
Iterator<Student> iterator = set.iterator();
if (iterator.hasNext()) {
Student type = (Student) iterator.next();
// System.out.println(s .equals(type.num));
// System.out.println(s.length());
System.out.println(type.num.length());
if (s .equals(type.num)) {
//System.out.println(s);
//System.out.println(type.num);
set.remove(type);
new Append().add(set);
System.out.println(type);
System.out.println("Has deletede it");
}else {
System.out.println("No this one");
}
}
我这代码.输入的和原有的 Student.num 是相同的,但是 有时候可以删除,有时候不能删除,有时候长度相同,有时候长度不同.为什么?
怎么办?
------解决方案--------------------
if (iterator.hasNext()) {
换为 while(iterator.hasNext())
if 只走一次,while会一直循环下去的