Results 1 to 4 of 4
Thread: Convert object to String
- 03-14-2010, 12:28 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 12
- Rep Power
- 0
Convert object to String
Hi,
need help, hope you will help me.
import java.util.Arrays;
import java.util.Comparator;
class LastFirstComparator implements Comparator {
public int compare(Object obj1, Object obj2) {
int result = 0;
int i = 0;
String[] str1 = (String[]) obj1; /// this seems not working, how to change to string type?
String[] str2 = (String[]) obj2; /// because it is working with String array.
do {
result = str1[i].compareTo(str2[i]);
i++;
if (i == 7) {
break;
}
} while (result == 0);
return result;
}
}
public class LexiComparator {
public static void main(String args[]) {
Integer a1[][] = {{13, -2, 3, 99, 0, 9, 45},
{2, 3, 9, 45, 4, 44, 21},
{22, 445, 0, -5, -56, 6, 223}}; ///if I pass it as String [][] = {{"13","-2"...},{"2","3",...},{...}}; it works, but I need to use Integer type 2d array.
Arrays.sort(a1, new LastFirstComparator());
}
}
Please give some ideas. Thanks
Out put:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Integer; cannot be cast to [Ljava.lang.String;
at finalproject.LastFirstComparator.compare(LexiCompa rator.java:23)...
- 03-14-2010, 12:43 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
- 03-14-2010, 12:54 PM #3
Member
- Join Date
- Mar 2010
- Posts
- 12
- Rep Power
- 0
why I did not think about it before, why I was trying to fix the object. it works now. thank you.
- 03-14-2010, 01:26 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
How to convert string array into object in java
By kgkamaraj in forum New To JavaReplies: 4Last Post: 02-12-2010, 12:33 PM -
How to convert a string array into object in java
By kgkamaraj in forum Advanced JavaReplies: 2Last Post: 02-12-2010, 09:12 AM -
convert a string to an object in java
By jforce93 in forum Advanced JavaReplies: 1Last Post: 08-09-2009, 11:57 PM -
Convert Comparable object to string or char
By ScKaSx in forum New To JavaReplies: 4Last Post: 01-25-2009, 02:02 PM -
Convert Linked List Object element to String
By CirKuT in forum New To JavaReplies: 2Last Post: 12-13-2008, 05:22 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks