Question:
How to fix this error in java programming?
Cutt
2010-02-10 12:08:00 UTC
I am trying to sort multiple objects that are stored in an array.
I keep getting the following error when trying to compile.

Note: my.java uses unchecked or unsafe operations;
Note: Recompile with -X1int: unchecked for details;

my class is using the following comparator method:

public static Comparator SortComparator = new Comparator ()
{
public int compare(Object customer, Object anotherCustomer)
{
String State1 = ((Customer) customer).getState().toUpperCase();
String Address1 = ((Customer) customer).getAddress().toUpperCase();
String State2 = ((Customer) anotherCustomer).getState().toUpperCase();
String Address2 = ((Customer) anotherCustomer).getAddress().toUpperCase();

if (!(State1.equals(State2)))
return State1.compareTo(State2);
else
return Address1.compareTo(Address2);
}
};


My driver that calls this method looks like this:

Arrays.sort(Data, Customer.SortComparator);

Every time I add this into my driver i get that error message.

Why and how do i fix this.
Thanks soo much
Four answers:
Mark aka jack573
2010-02-14 08:13:09 UTC
The reason is the Comparator should have a type of class it is used to compare.



For Instance, it should be:

public static Comparator SortComparator = new Comparator ()



In the method of the class, you should use Customer, not Object, like this:

public int compare(Customercustomer, Customer anotherCustomer)



Then you do not need to cast the Object to a Customer.



So you code should look like this:



public static Comparator SortComparator = new Comparator ()

{

public int compare(Customer customer, Customer anotherCustomer)

{

String State1 = customer).getState().toUpperCase();

String Address1 = customer).getAddress().toUpperCase();

String State2 = anotherCustomer).getState().toUpperCase(…

String Address2 = anotherCustomer).getAddress().toUpperCas…



if (!(State1.equals(State2)))

return State1.compareTo(State2);

else

return Address1.compareTo(Address2);

}

};



I may have removed some ( that you need, but since Yahoo has removed some of your code I could not tell.





Now, the reason for the warning that you get. It is only a warning, not an error. You do not have to fix it, but it is better to fix it if you can.



The newer versions of Java brought in more type checking so you could not pass a String object to your Comparator by mistake, and get a ClassCastExample. If you do the code as I have above, you can only put Customers into the method and there will be no mistake with the casting, as you are not casting.



If you want more information about this, it is called Generics. There is a whole section of the Java tutorial that explains Generics, and it is available here:

http://java.sun.com/docs/books/tutorial/extra/generics/index.html



Hope that helps.
mule
2010-02-10 12:22:49 UTC
instead of using a comparitor object, why not override the compareTo method in your Customer class?



In your Customer.java, put this



public int compareTo(Customer otherCustomer) {

String state1 = this.getState().toUpperCase();

String address1 = this.getAddress().toUpperCase();

String state2 = otherCustomer.getState().toUpperCase();

String address2 = otherCustomer.getAddress().toUpperCase();



if(!state1.equals(state2)) return state1.compareTo(state2);

else return address1.compareTo(address2);

} // end method int compareTo(Customer)



then, you can just do Arrays.sort(Data) without specifiying a sort comparator



cheers!





}
?
2016-05-31 02:30:13 UTC
There might be a bug. You might want to consider a System Restore to the earliest date then delete all the previous System Restore dates. You might want to save very important stuff. Also you might want to brush up on how to use the Windows file system(all the commands and manipulations on the keyboard. That is if you don't want to mess around with Java. If this does not work you might want to reinstall Windows. Or if you want to fix Java you might need to buy software(not my recommendation). You need to do also delete needless popups and junk that come up every day. Have you looked into Linux Ubuntu or Mint for Java?
mensah
2016-10-01 03:43:07 UTC
Uses Unchecked Or Unsafe Operations


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...