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

    Default convert obj to string

    how to convert obj to string for this method below :

    fish obj = (fish) x.get();
    throw new zoo(new Object[]{obj.getAnimal()});

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Override the toString method in the fish class.

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

    Default

    how? im new in programming.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Like so:

    Java Code:
    class Fish {
        private String name;
        public Fish(String name) {
            this.name = name;
        }
        public String getName() {
            return name;
        }
        public String toString() {
        	return "Fish["+getName()+"]";
        }  
        public static void main(String ... args) {
            Fish trout = new Fish("Large Mouth Trout");
            System.out.println("Trout as a string is :"+trout);
        }
    
    }

  5. #5
    MarsTeam is offline Member
    Join Date
    Jun 2010
    Posts
    8
    Rep Power
    0

    Default

    well I'm not certain as what you need to do, where do you want to convert it ?
    But you can do things like this:
    System.out.println(obj.ToString());

    If you want to override ToString method just add to fish class:
    Java Code:
        public String toString() {
            // Change code for your own but it should be simmilar to this
            return this.getName();
        }

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

    Default

    in the fish class, is String.

    here
    fish obj = (fish) x.get();
    throw new zoo(new Object[]{obj.getAnimal()});
    is in obj.

    i should convert obj to string cuz im getting classcastexception for this line

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Now is a good time to explain what you are trying to do with the line
    Java Code:
    throw new zoo(new Object[]{obj.getAnimal()});
    .

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

    Default

    getting back the parameter which i parse in

  9. #9
    MarsTeam is offline Member
    Join Date
    Jun 2010
    Posts
    8
    Rep Power
    0

    Default

    I have feeling you are doing something terribly wrong. You should either share more code, give us error messages, or at least try to explain in detail what's going on.

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

    Default

    error :
    X Servlet Error: java.util.ArrayList: java.lang.ClassCastException: java.util.ArrayList at com.fish.insertOthers
    cuz in the fish class is string.

    here is obj. how to convert from obj to string?
    Last edited by isme; 06-14-2010 at 09:51 AM.

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

    Default

    Quote Originally Posted by isme View Post
    how to convert from obj to string?

    general question, general answer:

    Object obj = new Object();
    String s = obj.toString();

    please, post the code that causes java.util.ArrayList at com.fish.insertOthers
    Last edited by j2me64; 06-14-2010 at 10:51 AM.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Yep.
    If you want help it would help if you actually posted the code throwing the exception, and not something seemngly unrelated.

    After all, the exception is complaining about ArrayList and not String.

Similar Threads

  1. How to convert a String into an Hexadecimal ?
    By ze snow in forum New To Java
    Replies: 7
    Last Post: 02-16-2010, 11:31 PM
  2. trying to convert an interger to a string
    By matpj in forum New To Java
    Replies: 12
    Last Post: 01-15-2009, 11:08 AM
  3. convert InetAddress to a string
    By hunterbdb in forum New To Java
    Replies: 4
    Last Post: 10-18-2008, 10:50 AM
  4. convert a really big string (len 39) to bigdecimal
    By coolsig in forum Advanced Java
    Replies: 6
    Last Post: 06-14-2008, 03:48 AM
  5. convert string to a double?
    By javaMike in forum Advanced Java
    Replies: 2
    Last Post: 11-27-2007, 04:10 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
  •