Java "compare cannot be resolved to a type" error
Posted
by
King Triumph
on Stack Overflow
See other posts from Stack Overflow
or by King Triumph
Published on 2012-09-25T03:31:45Z
Indexed on
2012/09/25
3:37 UTC
Read the original article
Hit count: 324
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);
© Stack Overflow or respective owner