Results 1 to 4 of 4
  1. #1
    innspiron is offline Member
    Join Date
    Mar 2010
    Posts
    12
    Rep Power
    0

    Default 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)...

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by innspiron View Post
    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)...
    Your Comparator expects two String arrays; you are passing Integer arrays so the cast fails and the jvm says so; change your Comparator (to make it expect Integer[] instead) and all will be fine.

    kind regards,

    Jos

  3. #3
    innspiron is offline Member
    Join Date
    Mar 2010
    Posts
    12
    Rep Power
    0

    Default

    why I did not think about it before, why I was trying to fix the object. it works now. thank you.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by innspiron View Post
    why I did not think about it before, why I was trying to fix the object. it works now. thank you.
    If you had used generics the compiler could've assisted you somewhat, e.g.

    Java Code:
    class LastFirstComparator implements Comparator<Integer[]> {
    	
    	public int compare(Integer[] a, Integer[] b) {
    		
    		// compare a and b here ...
    	}
    }
    kind regards,

    Jos

Similar Threads

  1. How to convert string array into object in java
    By kgkamaraj in forum New To Java
    Replies: 4
    Last Post: 02-12-2010, 12:33 PM
  2. How to convert a string array into object in java
    By kgkamaraj in forum Advanced Java
    Replies: 2
    Last Post: 02-12-2010, 09:12 AM
  3. convert a string to an object in java
    By jforce93 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 11:57 PM
  4. Convert Comparable object to string or char
    By ScKaSx in forum New To Java
    Replies: 4
    Last Post: 01-25-2009, 02:02 PM
  5. Convert Linked List Object element to String
    By CirKuT in forum New To Java
    Replies: 2
    Last Post: 12-13-2008, 05:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •