Results 1 to 5 of 5
  1. #1
    ScKaSx is offline Member
    Join Date
    Jan 2009
    Posts
    7
    Rep Power
    0

    Default Convert Comparable object to string or char

    Hi All,

    I have a Link List of comparables that I would like to convert into one string:

    I am doing something like:

    Java Code:
    char[] LinkedListArray = null;
    int i = 0;
    
    while (not_end_of_linked_list) {
         LinkedListArray[i] = item;
         i++;
         next_in_LinkedList();
    }
    where item is a comparable that I would like to convert to a string or char. What is the best way to do this?

    Cheers,
    ScKaSx

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Please tell us what you are trying to accomplish. Your code takes each object and reduces it to a single char, which you place in an array. Not to mention you never actually created the array.

    Are you wanting the save the state, or all the properties, of each object?

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    I have no idea what you're trying to accomplish with this, but you can certainly add a toString method to your Comparable implementation.

  4. #4
    ScKaSx is offline Member
    Join Date
    Jan 2009
    Posts
    7
    Rep Power
    0

    Default

    Basically I have a linked list of comparable objects that I want to concatenate together in a string. For instance, the linked list might look like:

    comparable: Adam
    |
    v
    comparable: Mike
    |
    v
    comparble: Tom

    I am looping through the linked list and for each comparable item I want to concatenate the comparable into a string so that *.toString() will give:

    Adam
    Mike
    Tom

    Any ideas on the correct implementation?

    My original code has changed to one using concatenation of a String class:

    Java Code:
    String LLstring = "";
    Comparable item;
     
    while (not_end_of_linked_list) {
         LLstring = LLstring + item;
         next_in_LinkedList();
    }
    However, this doesn't print out the elements of the linked list.

    Cheers,
    ScKaSx
    Last edited by ScKaSx; 01-25-2009 at 09:34 AM.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    you might need to call toString() explicitly on the item in the loop:

    Java Code:
       LLstring += item.toString(); // + "\n"; ???
    Also, if you're doing a lot of these concatenations, then use a StringBuilder and call its append(...) method.
    Last edited by Fubarable; 01-25-2009 at 02:04 PM.

Similar Threads

  1. Convert Linked List Object element to String
    By CirKuT in forum New To Java
    Replies: 2
    Last Post: 12-13-2008, 05:22 AM
  2. Replies: 9
    Last Post: 09-11-2008, 02:51 AM
  3. char to string
    By kian_hong2000 in forum New To Java
    Replies: 2
    Last Post: 08-25-2008, 01:51 PM
  4. Creating a Comparable object
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 07:38 PM
  5. Cannot convert from char to String error
    By sondratheloser in forum New To Java
    Replies: 1
    Last Post: 12-13-2007, 09:28 PM

Posting Permissions

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