import java.io.BufferedReader; import java.util.Collections; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Comparator; import java.util.Date; import java.util.List; import com.javaranch.common.TextFileIn; public class SortNames { public static class CelebrityNamesFile { public String firstName; public String lastName; public static class CompareLastName implements Comparator< CelebrityNamesFile > { @Override public int compare( CelebrityNamesFile o1, CelebrityNamesFile o2 ) { return o2.lastName.compareTo( o1.lastName ); } } public static void main( String[ ] args ) throws IOException { ArrayList< CelebrityNamesFile > myCelebrityList; myCelebrityList = new ArrayList< CelebrityNamesFile >(); TextFileIn celebrityNamesFile = new TextFileIn( "celebrityNamesFile.txt" ); boolean doneReadingCelebrityNames = false; while ( ! doneReadingCelebrityNames ) { String oneName = celebrityNamesFile.readLine(); if ( oneName == null ) { doneReadingCelebrityNames = true; }
$ Eclipse does not like the following add statement, namely: the add method (SortNames.CelebrityNamesFile) of type ArrayList (SortNames.CelebrityNamesFile) is not applicable for arguments (String)
else { myCelebrityList.add( oneName ); } } celebrityNamesFile.close();
$ And then it doesnβt like my query of the form, namely: Associated mismatch: the general method sort (List T) of type Collections is not applicable for arguments (ArrayList (SortNames.CelebrityNamesFile)). The deduced type SortNames.CelebrityNamesFile is not a valid substitute for a limited parameter (T extends Comparable (? Super T))
Collections.sort( myCelebrityList ); System.out.println( myCelebrityList ); Collections.sort( myCelebrityList, new CelebrityNamesFile.CompareLastName() ); System.out.println( myCelebrityList ); } }
}
What am I doing wrong? I read a lot of posts here, read Java docs regarding comparator, read Java tutorials on comparator. Head First Java, Ivor Horton, the beginner of Java 7. Still clueless.
The purpose of the program is to read the names from the txt file, add them to arraylist, print arithmetic in natural order, sort arraylist by last name, print the list again.
source share