简体   繁体   中英

How do I fix my sortData and compareTo method. I keep receiving “cannot be converted to error.”

I keep receiving error: incompatible types: BingoPlayer cannot be converted to BingoPlayer[] when I try to pass to arrays to my compareTo method. I am not really sure how to fix the issue.

I am reading data from a text file. Here are the contents of that file:

  • 50.00
  • 10
  • James,Smith,50.0
  • Michael,Smith,50.0
  • Robert,Smith,50.0
  • Maria,Garcia,50.0
  • David,Smith,50.0
  • Maria,Rodriguez,50.0
  • Mary,Smith,50.0
  • Maria,Hernandez,50.0
  • Maria,Martinez,50.0
  • James,Clapper,50.0

import java.io. ; import java.util. ;

public static void sortData()
{
    int n = test.length;
    int m =0;
    BingoPlayer temp;  
     for(int i=0; i < n; i++)
     {  
        for(int j=1; j < (n-i); j++)
        {  
            m = compareTo((test[j], test[j+1];
           if(m > 0)
           {  
             temp = test[j-1];  
             test[j-1] = test[j];  
             test[j] = temp;  
           }  

        }  
     }  

}
public static int compareTo(BingoPlayer[] player1, BingoPlayer[] player2)
{
    for(int i =0; i < 10; i++)
    {
            if(player1[i].firstName.compareTo(player2[i].firstName) != 0)
                return player1[i].firstName.compareTo((player2[i].firstName));
            else if (player1[i].lastName.compareTo(player2[i].lastName) != 0)
                return player1[i].lastName.compareTo((player2[i].lastName));
            else 
                return player1[i].lastName.compareTo(player2[i].lastName);
    }

}

}

Since you didn't provide all of your code, I am going to assume "test" is an array of BingoPlayer objects.

I suggest that you change your compareTo method to only compare 1 BingoPlayer with another BingoPlayer. With the way that your code works you are comparing everything in test and then calling a method which compares everything in test again. Let's reduce the redundancy and try to make this work:

public static void sortData()
{
    int n = test.length;
    int m =0;
    BingoPlayer temp;  
     for(int i=0; i < n; i++)
     {  
        for(int j=1; j < (n-i); j++)
        {  
            m = compareTo((test[j], test[j+1];
           if(m > 0)
           {  
             temp = test[j-1];  
             test[j-1] = test[j];  
             test[j] = temp;  
           }  

        }  
     }  

}
public static int compareTo(BingoPlayer player1, BingoPlayer player2)
{

            if(player1.firstName.compareTo(player2.firstName) != 0)
                return player1.firstName.compareTo((player2.firstName));
            else if (player1.lastName.compareTo(player2.lastName) != 0)
                return player1.lastName.compareTo((player2.lastName));
            else 
                return player1.lastName.compareTo(player2.lastName);

}

}

Again, I had to assume that "test" was an array of BingoPlayer objects. If that is the case, then you were trying to send two single BingoPlayer objects to a method that was expecting two BingoPlayer arrays. Making the change to your compareTo that I show above should fix your program.

Your compare method should take two BingoPlayer object, not array of those.

You should also read about Comparable and Comparator interfaces.

EDIT: There is some snippet with Comparable and Comparator implementation

class BingoPlayer implements Comparable<BingoPlayer> {

    // rest of your class
    String firstName, lastName;

    @Override
    public int compareTo(BingoPlayer player) {
        if(firstName.compareTo(player.firstName) != 0)
            return firstName.compareTo((player.firstName));
        else if (lastName.compareTo(player.lastName) != 0)
            return lastName.compareTo((player.lastName));
        else
            return lastName.compareTo(player.lastName);
    }
}

And you use it like this:

player1.compareTo(player2);

Other way is create own Comparator class:

class BingoPlayerComparator implements Comparator<BingoPlayer> {

    @Override
    public int compare(BingoPlayer player1, BingoPlayer player2) {
        if(player1.firstName.compareTo(player2.firstName) != 0)
            return player1.firstName.compareTo((player2.firstName));
        else if (player1.lastName.compareTo(player2.lastName) != 0)
            return player1.lastName.compareTo((player2.lastName));
        else 
            return player1.lastName.compareTo(player2.lastName);
    }
}

Which can be used like this:

BingoPlayerComparator c = new BingoPlayerComparator();
c.compare(player1, player2);

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