简体   繁体   中英

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.

I created an address book. One of the requirements for my address book is to compare two people by last name. This is the method I wrote to accomplish that goal. However, it's case sensitive when used, so I tried writing a warning to the user.

But the warning prints regardless of whether the contacts are found in the arrayOfPersons . So I think that my boolean is not updating correctly or the way I'm checking to see if the two names exist in the persons array is wrong? Is that right?

Have you tried doing like this ?

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

}

Your if statement in the for loop will never be true unless lname1 and lname2 are equals. I don't know if what you did is what you wanted to do. You can do like this which is similar to your code you already have:

In the compareTo method check if the arrayOfPersons contains those two Persons

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

and then compare lname1 and lname2 like you did with your last three if statements

Note that to use the contains method you need to ovverride in your Person class the equals method

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

 }

}

I figured it out. This is what I was looking for. Thanks for the pointers everybody. Similar to solution to what Shreshta proposed, just had to modify his logic a little bit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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