#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    15
    Rep Power
    0

    Casting Vector to String


    Hi All,

    I am getting a classcastexception error when I try to cast a vector to a string, which I thought was possible.

    Can anyone assist me please?

    ResultSet rsFaculty = faculty.getNewFaculty();
    Vector v = new Vector();
    while (rsFaculty.next();
    {
    Vector row = new Vector();
    row.add(rsFaculty.getString("FacultyID"));
    row.add(rsFaculty.getString("FacultyName"));
    v.add(row);
    }
    int vsize = v.size();
    int choice = (int)(Math.random()*vsize);

    String StringName = (String) v.elementAt(2);

    out.println(v.elementAt(2));
  2. #2
  3. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    You cannot cast a Vector to a String. It is nothing like a string. Even though you have Strings in it, the Vector itself is completely different. You could call the toString() method of Vector, but that would likely only give you the memory address of the object and nothing else. To get to your strings you need to dereference twice. Once for the first Vector and again for the strings inside.

    Your code has typo's and is a little confusing. What are you trying to accomplish in detail?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    15
    Rep Power
    0
    Thanks for your reply Nemi.

    I got it working earlier.

    Regards,
    CC

IMN logo majestic logo threadwatch logo seochat tools logo