繁体   English   中英

CompareTo方法无法按我的通讯录的预期工作

[英]CompareTo method not working as expected for my address book

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中找到联系人,都会打印警告。 因此,我认为我的布尔值未正确更新,或者我检查在persons数组中是否存在两个名称的方式是否错误? 那正确吗?

您是否尝试过这样做?

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 + " .");

}

除非lname1和lname2相等,否则for循环中的if语句永远不会为真。 我不知道您所做的就是您想做的。 您可以这样做,类似于您已有的代码:

在compareTo方法中,检查arrayOfPersons包含这两个Persons

if(arrayOfPersons.contains(Person1) && arrayOfPersons.contains(Person2)

然后像对最后三个if语句一样比较lname1和lname2

请注意,要使用contains方法,您需要在Person类中使用equals方法

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提出的解决方案类似,只需要稍微修改一下他的逻辑即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM