Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Convert object to String

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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}};
            Arrays.sort(a1, new LastFirstComparator());
        }
    }
    Please give some ideas. Thanks
    Last edited by helloworld922; March 14th, 2010 at 11:45 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Convert object to String

    The class Object has a method toString() which will return a string that describes the object. By default, this returns:
    getClass().getName() + '@' + Integer.toHexString(hashCode())
    Programmers can override this method in their objects to return a more customized description of an object.

  3. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Convert object to String

    What exception do you get? A class cast exception?

    // Json

Similar Threads

  1. Create a CLOB object with the string value
    By oshoarun in forum JDBC & Databases
    Replies: 0
    Last Post: March 6th, 2010, 02:54 AM
  2. How to convert a String into an Hexadecimal ?
    By lumpy in forum Java Theory & Questions
    Replies: 2
    Last Post: February 16th, 2010, 05:01 PM
  3. Conversion of string into integer in Java
    By JavaPF in forum Java Programming Tutorials
    Replies: 17
    Last Post: January 23rd, 2010, 09:33 AM
  4. Convert string to int?
    By connex in forum Java Theory & Questions
    Replies: 1
    Last Post: December 9th, 2009, 05:06 AM
  5. Convert CHAR to STRING
    By fh84 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: October 29th, 2009, 09:21 PM