This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Cattle Drive and the fly likes Warning error implementing Comparator Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Reply Bookmark "Warning error implementing Comparator" Watch "Warning error implementing Comparator" New topic
Author

Warning error implementing Comparator

Gary Ba
Ranch Hand

Joined: Oct 23, 2009
Posts: 150

Hi,
I am trying to implement Comparator on a program and it is giving me an warning error. Here is the warning...

SortNames.java:60: warning: [unchecked] unchecked conversion
found : FirstNameComparator
required: java.util.Comparator<? super Person>
Arrays.sort(persons, new FirstNameComparator());
^
SortNames.java:60: warning: [unchecked] unchecked method invocation: <T>sort(T[],java.util.Comparator<? super T>)
java.util.Arrays is applied to (Person[],FirstNameComparator)
Arrays.sort(persons, new FirstNameComparator());


Person[] persons
class FirstNameComparator implements Comparator {}

If more information is need, I can send the code.

can someone please help me point out what I am doing wrong here?
garry


star chaser..
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9033
    
  10
You're not doing anything "wrong". You're using Java 5 or higher to compile code that is 1.4-style code. I think there is a flag you can use ... -source or -target ... or both to avoid the warning message. Otherwise, you can ignore it (for now).


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Gary Ba
Ranch Hand

Joined: Oct 23, 2009
Posts: 150

I am not!
Yes I saw the @suppress something online where compiler will ignore the compile warnings.
That should be on the assignment note for others...and me

Thank you,
garry
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Garry Ba wrote:I am not!
Yes I saw the @suppress something online where compiler will ignore the compile warnings.
That should be on the assignment note for others...and me


When I was doing the drive (briefly), we were instructed to compile and run using Java 1.4. Is that still a requirement? If so, this is a non-issue.
Gary Ba
Ranch Hand

Joined: Oct 23, 2009
Posts: 150

Here is what the requirements says on the Cattle Drive.

Download the Java SE Development Kit 6 from Sun.

For once I am right on this.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9033
    
  10
... but it needs to be compilable/runnable with Java 1.4
Gary Ba
Ranch Hand

Joined: Oct 23, 2009
Posts: 150

Hi,
You guys/gals know more than I do but I do not think it is explicitly written in the Cattle Drive section.


Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9033
    
  10
If you type

javac -help

at the command prompt, you see:

Usage: javac <options> <source files>
where possible options include:
-g Generate all debugging info
-g:none Generate no debugging info
-g:{lines,vars,source} Generate only some debugging info
-nowarn Generate no warnings
-verbose Output messages about what the compiler is doing
-deprecation Output source locations where deprecated APIs are used
-classpath <path> Specify where to find user class files
-sourcepath <path> Specify where to find input source files
-bootclasspath <path> Override location of bootstrap class files
-extdirs <dirs> Override location of installed extensions
-d <directory> Specify where to place generated class files
-encoding <encoding> Specify character encoding used by source files
-source <release> Provide source compatibility with specified release
-target <release> Generate class files for specific VM version
-help Print a synopsis of standard options
Gary Ba
Ranch Hand

Joined: Oct 23, 2009
Posts: 150

I tried .......

javac SortNames.java -source 1.4

And I get this messege...

SortNames.java:31: generics are not supported in -source 1.4
(use -source 5 or higher to enable generics)
ArrayList<String> names = new ArrayList<String>();
^
1 error

and when I tried....

javac SortNames.java -source 1.6

I get this...

Note: SortNames.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

finally tried this...

javac SortNames.java -Xlint

I get this..

SortNames.java:51: warning: [unchecked] unchecked conversion
found : LastNameComparator
required: java.util.Comparator<? super java.lang.String>
Collections.sort(names, new LastNameComparator());
^
SortNames.java:51: warning: [unchecked] unchecked method invocation: <T>sort(java.util.List<T>,java.util.Compar
super T>) in java.util.Collections is applied to (java.util.ArrayList<java.lang.String>,LastNameComparator)
Collections.sort(names, new LastNameComparator());
^
2 warnings


At the end, I still the @SuppressWarnings("unchecked").


-garry
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 19672
    ∞

For assignments you send to me, using 1.5 syntax is okay, as long as you don't use autoboxing or some other things I don't want you guys to use.

(some of these things are fine to use in a professional environment, but while you are learning, using these things can lead to confusion)

Generics is fine by me.

Do not suppress the warnings.


permaculture forums
Gary Ba
Ranch Hand

Joined: Oct 23, 2009
Posts: 150

"Make the code so that you don't get warnings." Fixed it.
java introduces generics in java 1.5 and I did not put it into account ie. Comparator<Object>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Warning error implementing Comparator
 
Similar Threads
Unchecked cast/conversion warnings
accessibility modifiers for classes
warnings while sorting using Collections class
How to sort existing TreeSets with a Comparator?
Problems with Compiling Generics.