• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Warning error implementing Comparator

 
Ranch Hand
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 9099
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Gary Ba
Ranch Hand
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9099
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... but it needs to be compilable/runnable with Java 1.4
 
Gary Ba
Ranch Hand
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9099
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Trailboss
Posts: 23216
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Gary Ba
Ranch Hand
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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>
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic