public void compareTo(String lname1, String lname2) {
/* Note to self: Using this method is case sensitive, because
it only prints if names are found in the array. And those names
are case sensitive inside the array, even though I'm using the
CompareTo method from java's String
class which is NOT inherently case sensitive. ???????? */
boolean foundContact = false;
for(int i = 0; i < arrayOfPersons.size(); i++){
if(arrayOfPersons.get(i).getFname().equals(lname1) && (arrayOfPersons.get(i).getFname().equals(lname2))) {
lname1.compareTo(lname2);
foundContact = true;
}
}
if (foundContact == false)
System.out.println("This option is case sensitive. Check your spelling and try again. Otherwise these contacts do not exist.");
if(lname1.compareTo(lname2) < 0)
System.out.println(lname1 + " comes after " + lname2 + " .");
if(lname1.compareTo(lname2) == 0)
System.out.println(lname1 + " are equal " + lname2 + ".");
if(lname1.compareTo(lname2) > 0)
System.out.println(lname1 + " comes before " + lname2 + " .");
}
case 6:
System.out.println("Enter last name #1:");
String lname3 = scnr.next();
System.out.println("Enter last name #2:");
String lname4 = scnr.next();
Necronomicon.compareTo(lname3, lname4);
break;
// This case is from my main and shows how I use the compareTo method. Just one of many options to my address book.
我创建了一个地址簿。我的地址簿的一个要求是按姓氏比较两个人。这是我为实现这一目标而编写的方法。但是,它在使用时区分大小写,因此我尝试向用户写一个警告。
但无论是否在arrayOfPersons
中找到联系人,都会打印警告。所以我认为我的布尔值没有正确更新,或者我检查人员数组中是否存在这两个名称的方式是错误的?是吗?
答案 0 :(得分:0)
你试过这样做吗?
boolean foundlname1 = false,foundlname2 = false;
for(int i = 0; i < arrayOfPersons.size(); i++)
{
if(arrayOfPersons.get(i).getFname().equals(lname1) && !foundlname1)
foundlanme1 = true;
if(arrayOfPersons.get(i).getFname().equals(lname2) && !foundlname2)
foundlanme2 = true;
if(foundlanme1 && foundlanme2)
{
foundContact = true;
break;
}
}
if (foundContact == false)
System.out.println("This option is case sensitive. Check your spelling and try again. Otherwise these contacts do not exist.");
else if(lname1.compareToIgnoreCase(lname2) > 0)
System.out.println(lname1 + " comes after " + lname2 + " .");
else if(lname1.compareToIgnoreCase(lname2) == 0)
System.out.println(lname1 + " are equal " + lname2 + ".");
else
System.out.println(lname1 + " comes before " + lname2 + " .");
}
答案 1 :(得分:0)
除非lname1和lname2等于,否则for循环中的if语句永远不会为真。我不知道你做了什么是你想做的。您可以这样做,这类似于您已有的代码:
在compareTo方法中检查arrayOfPersons
是否包含这两个人
if(arrayOfPersons.contains(Person1) && arrayOfPersons.contains(Person2)
然后比较lname1和lname2,就像你在最后三个if语句中所做的那样
请注意,要使用contains方法,需要在Person类中使用equals方法
进行ovverride答案 2 :(得分:0)
public void compareTo(String lname1, String lname2) {
boolean foundContact1 = false;
boolean foundContact2 = false;
for(int i = 0; i < arrayOfPersons.size(); i++){
if(arrayOfPersons.get(i).getLname().equals(lname1)) {
foundContact1 = true;
}
}
for(int i = 0; i < arrayOfPersons.size(); i++){
if(arrayOfPersons.get(i).getLname().equals(lname2)) {
foundContact2 = true;
}
}
if (foundContact1 && foundContact2 == false)
System.out.println("This option is case sensitive. Check your spelling and try again. Otherwise these contacts do not exist.");
if(foundContact1 && foundContact2 == true) {
if(lname1.compareTo(lname2) < 0)
System.out.println(lname1 + " comes after " + lname2 + " .");
else if(lname1.compareTo(lname2) == 0)
System.out.println(lname1 + " are equal " + lname2 + ".");
else if(lname1.compareTo(lname2) > 0)
System.out.println(lname1 + " comes before " + lname2 + " .");
}
}
我明白了。这就是我想要的。感谢大家的指点。类似于Shreshta提出的解决方案,只需稍微改变他的逻辑。