Results 1 to 20 of 20
  1. #1
    isme is offline Member
    Join Date
    Apr 2010
    Posts
    18
    Rep Power
    0

    Default convert string to list

    how to convert string to java.util.list, to return back as a list

    String obj = (String) list1.get(0);

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    isme is offline Member
    Join Date
    Apr 2010
    Posts
    18
    Rep Power
    0

    Default

    from this class it return as java.util.list
    .
    .
    .
    .
    .
    while(rs.next()){
    List list = new ArrayList();
    String x1 = rs.getString("x");
    list.add(x1);

    list1.add(list);
    now need to convert it so that it return as well as java.util.list to here..but how?

    String obj = (String) list1.get(0);

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Well, you can't just convert a List to a String. You have added your List list to what appears to be another List list1. So you can't just pull a String off of list1 -- you have to pull a List off, and then you can pull a String out of that.

    If you share more of your code, maybe we can help you find where you are getting confused.

    -Gary-

  5. #5
    isme is offline Member
    Join Date
    Apr 2010
    Posts
    18
    Rep Power
    0

    Default

    String obj = (String) list1.get(0);
    this line is giving me java.lang.ClassCastException

    i do not know how to convert it

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by isme View Post
    this line is giving me java.lang.ClassCastException

    i do not know how to convert it
    As I told you, you can't convert it. You didn't add() a String to list1, you added a List to it.

    Java Code:
    String x1 = rs.getString("x");
    list.add(x1);
    
    [COLOR="Red"]list1.add(list);
    [/COLOR]
    So you can't get() a String from there -- you can only get() a List -- and then that List has a String on it.

    -Gary-

  7. #7
    isme is offline Member
    Join Date
    Apr 2010
    Posts
    18
    Rep Power
    0

    Default

    still having error here
    String obj = (String) list1.get(0);

  8. #8
    isme is offline Member
    Join Date
    Apr 2010
    Posts
    18
    Rep Power
    0

    Default

    protected List....
    .
    .
    List list1 = new ArrayList();
    .
    .
    while(rs.next()){
    List list = new ArrayList();
    String x1 = rs.getString("x");
    list.add(x1);

    list1.add(list);
    in other class :
    protected void ....
    if (list1 !=null && list1.size() >0)
    { String obj = (String) list1.get(0);
    those read line giving me error on java.lang.ClassCastException

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Instead of using ArrayList and List, use the proper format ArrayList<String>, ArrayList<WhateverClassYouAreStoring>, that sort of thing.

    That way you might actually find out what you are storing in the List.

    You have been told several times now that you are trying to cast a List (which is what list1.get(0) is returning) into a String, and that is an illegal operation.

  10. #10
    isme is offline Member
    Join Date
    Apr 2010
    Posts
    18
    Rep Power
    0

    Default

    so what exactly the correct way should i code?

  11. #11
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by isme View Post
    so what exactly the correct way should i code?

    example


    Java Code:
    import java.util.ArrayList;
    import java.util.List;
    
    public class ListExample {
    	public static void main(String[] args) {
    
    		List<String> list = new ArrayList<String>();
    		list.add("String 1");
    		list.add("String 2");
    		String s = list.get(0);
    		System.out.println(s);
    	}
    }

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Quote Originally Posted by isme View Post
    so what exactly the correct way should i code?
    Who knows?
    We don't know your requirements.

    You seem to be getting stuff out of a db and sticking them into a List of Lists of Strings. Which to me flags a warning that you should be creating objects of some sort of class that contains fields that represent your data.

  13. #13
    isme is offline Member
    Join Date
    Apr 2010
    Posts
    18
    Rep Power
    0

    Default

    yes thats true.

  14. #14
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    832
    Rep Power
    6

    Default

    that too in you code snippet you are creating the frest list for each and every traversal in ResultSet loop.
    How will it maintain all the elements?
    Ramya:cool:

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    They're populating a List<List<String>>, though it's not declared as such.
    So the List created in the loop through the resultset is the inner list, one for each result.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    String obj = (String) list1.get(0)
    Perhaps you could see what list1.get(0) is by using println() to show it before doing the assignment.

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    It's a List.
    from earlier -
    Java Code:
    List list1 = new ArrayList();
    .
    .
    while(rs.next()){
        List list = new ArrayList();
        String x1 = rs.getString("x");
        list.add(x1);
    
        list1.add(list);
    }
    ETA: They're basically taking the resultset and turning each row in to a List<String>, and sticking them into a List<List<String>>. All because, I presume, they can't be bothered to create a proper object for the data.

  18. #18
    isme is offline Member
    Join Date
    Apr 2010
    Posts
    18
    Rep Power
    0

    Default

    so what should i change to return back same as the earlier code for this line?

    String obj = (String) list1.get(0)

  19. #19
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by isme View Post
    so what should i change to return back same as the earlier code for this line?
    Java Code:
            .
            .
            .
            List newList = (List)(list1.get(0));
            String obj = (String)(newList.get(0));
            .
            .
            .
    That's a best guess based on the code you've shown us, but you've never shown us all the code, so I can't be sure if it's what you want. But more importantly, you really should review the concepts here, and investigate the generic forms of List and ArrayList.

    -Gary-

  20. #20
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Yes.
    Without the requirements, essentially, we can't give you a reasonable solution.
    As I said above, it looks to me like you are avoiding writing Java properly. List<List<String>> created from a ResultSet is usually a sign you're missing a class somewhere, and it should be List<MyClass>.

Similar Threads

  1. convert obj to string
    By isme in forum New To Java
    Replies: 11
    Last Post: 06-14-2010, 10:54 AM
  2. convert number to string
    By wizard_oz in forum New To Java
    Replies: 9
    Last Post: 11-16-2009, 07:04 PM
  3. Convert Linked List Object element to String
    By CirKuT in forum New To Java
    Replies: 2
    Last Post: 12-13-2008, 05:22 AM
  4. convert InetAddress to a string
    By hunterbdb in forum New To Java
    Replies: 4
    Last Post: 10-18-2008, 09:50 AM
  5. How to convert List to Array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:37 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
  •