1

I'm getting a strange error when attempting to use a comparator with a binary search on an array. The error states that "compareArtist cannot be resolved to a type" and is thrown by Eclipse on this code:

Comparator<Song> compare = new Song.compareArtist();

I've done some searching and found references to a possible bug with Eclipse, although I have tried the code on a different computer and the error persists.

I've also found similar issues regarding the capitalization of the compare method, in this case compareArtist. I've seen examples where the first word in the method name is capitalized, although it was my understanding that method names are traditionally started with a lower case letter. I have experimented with changing the capitalization but nothing has changed.

I have also found references to this error if the class doesn't import the correct package. I have imported java.util in both classes in question, which to my knowledge allows the use of the Comparator.

I've experimented with writing the compareArtist method within the class that has the binary search call as well as in the "Song" class, which according to my homework assignment is where it should be. I've changed the constructor accordingly and the issue persists.

Lastly, I've attempted to override the Comparator compare method by implementing Comparator in the Song class and creating my own method called "compare". This returns the same error. I've only moved to calling the comparator method something different than "compare" after finding several examples that do the same.

Here is the relevant code for the class that calls the binary search that uses the comparator. This code also has a local version of the compareArtist method. While it is not being called currently, the code for this method is the same as the in the class Song, where I am trying to call it from.

Thanks for any advice and insight.

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

public class SearchByArtistPrefix {

    private Song[] songs;  // keep a direct reference to the song array
    private Song[] searchResults;  // holds the results of the search
    private ArrayList<Song> searchList = new ArrayList<Song>();  // hold results of       search while being populated. Converted to searchResults array.

    public SearchByArtistPrefix(SongCollection sc) {
        songs = sc.getAllSongs();
    }

   public int compareArtist (Song firstSong, Song secondSong) {
      return firstSong.getArtist().compareTo(secondSong.getArtist());
   }

    public Song[] search(String artistPrefix) {

       String artistInput = artistPrefix;
      int searchLength = artistInput.length();

      Song searchSong = new Song(artistInput, "", "");
      Comparator<Song> compare = new Song.compareArtist();
      int search = Arrays.binarySearch(songs, searchSong, compare);

Code for Song class:

import java.io.FileNotFoundException;
import java.util.*;

public class Song implements Comparable<Song> {

   private String artist, title, lyrics;
   private static int compareCounter = 0;

   public Song(String artistName, String songTitle, String songLyrics) {
      this.artist = artistName;
      this.title = songTitle;
      this.lyrics = songLyrics;
   }
   public String getArtist() {
      return artist;
   }

   public void setArtist(String artist) {
      this.artist = artist;
   }

   public String getTitle() {
      return title;
   }

   public void setTitle(String title) {
      this.title = title;
   }

   public String getLyrics() {
      return lyrics;
   }

   public void setLyrics(String lyrics) {
      this.lyrics = lyrics;
   }  

   public static int getCounter() {
      return compareCounter;
   }

   public static void setCounter(int compareCounter) {
      Song.compareCounter = compareCounter;
   }

   public int compareTo(Song secondSong) {

      String secondArtist = secondSong.getArtist();
      String secondTitle = secondSong.getTitle();

      compareCounter++;

      if (!this.artist.equals(secondArtist)) {
         return this.artist.compareToIgnoreCase(secondArtist);
      } else {
         return this.title.compareToIgnoreCase(secondTitle);
      }
   }

   public int compareArtist (Song firstSong, Song secondSong) {
      return firstSong.getArtist().compareTo(secondSong.getArtist());
   }

There is also a main() method at the end of the class, but it only provides internal testing code and isn't relevant to the problem at hand.

0

2 Answers 2

1

In this code:

Comparator<Song> compare = new Song.compareArtist();

you are instantiating from Song.compareArtist static inner class. This error message says it cannot find this class. For this to work, you must change your Song class in this way:

class Song{

  public static class compareArtist<T> implements Comparator<T> {

    @Override
    public int compare(Object o1, Object o2) {
        // TODO Auto-generated method stub
        return 0;
    }

  }

}
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you, that worked. I didn't realize I had to create a subclass in the class Song for the comparator to work. Now I just have to figure out how to pass in my getter from the main class...
Strange. It worked for a moment. The error was gone and then Eclipse decided not to like it again.
I am pretty sure that after it worked, you changed something else but perhaps not in the right way. Please put the error message here to see if I can find the error.
The error message was the same. "Could not be resolved to a type". The only difference is that is that I used: public static class CompareArtist implements<Song> { because when I used: public static class CompareArtist<Song> implements<Song> { I got an error about hiding the type.
I do appreciate your help. Thank you.
|
0

Your statement:

Comparator<Song> compare = new Song.compareArtist();

Means that there is inner class named compareArtist in class Song. But I think you only want to use the method named compareArtist in class SearchByArtistPrefix. If it is so, You should do it as follow , and use anonymous inner class.

First, Remove the compareArtist method

Second, Implement the Comparator interface

Comparator<Song> compare = new Comparator<Song>(){

    public int compareTo(Song anotherSong){
        //Implement your compare logic here
    }

};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.