how to convert obj to string for this method below :
fish obj = (fish) x.get();
throw new zoo(new Object[]{obj.getAnimal()});
Printable View
how to convert obj to string for this method below :
fish obj = (fish) x.get();
throw new zoo(new Object[]{obj.getAnimal()});
Override the toString method in the fish class.
how? im new in programming.
Like so:
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);
}
}
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:
Code:public String toString() {
// Change code for your own but it should be simmilar to this
return this.getName();
}
in the fish class, is String.
hereis in obj.Quote:
fish obj = (fish) x.get();
throw new zoo(new Object[]{obj.getAnimal()});
i should convert obj to string cuz im getting classcastexception for this line
Now is a good time to explain what you are trying to do with the line
.Code:throw new zoo(new Object[]{obj.getAnimal()});
getting back the parameter which i parse in
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.
error :
cuz in the fish class is string.Quote:
X Servlet Error: java.util.ArrayList: java.lang.ClassCastException: java.util.ArrayList at com.fish.insertOthers
here is obj. how to convert from obj to string?
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.