Henry Wong wrote:What version of Java are you using?
Henry Wong wrote:
When you try to print any object, that is not a string, the println() method will call the toString() method of the object, in order to get the string representation, so that it can print it.
Since your Data class, did not implement the toString() method, it will inherit it from its super class. And with the Object class, the toString() method returns the name of the class along with the Identity hash for the instance.
Henry
Dave Tolls wrote:
orry kaplan wrote:[
This is all of the code i have.
How can this be all the code?
There isn't even a class definition.
Carey Brown wrote:
orry kaplan wrote:This is all of the code i have. And my error message i got is, " The method PrintDataArray(ArrayList<Data>) is undefined for the type PJ1"
What is PJ1 ?
Carey Brown wrote:2 things would help us help you: 1) show all of your code, 2) copy-n-paste error message(s) into your post.
PrintDataArray(ArrayA);
Do you have such a method? What argument is the method expecting?
Ole Sandum wrote:Not using the wrapper classes, no. But it is possible the paper used the primitive types long and float (lowercase l and f), which can be converted between eachother. Are you sure you quoted the paper directly without changing the case of those letters?
Ole Sandum wrote:Here you are trying to cast a reference of type Long to Float.
Note that to be able to successfully cast a reference to a type T, the object referred to by said variable must be of a subtype of T (or of T itself). The compiler is clever enough to see that this will never be the case for Float and Long, and does not allow the code to compile.
Ole Sandum wrote:Hi, orry.
Primitives cannot be cast the same way reference variables can. Maybe you are talking about primitive type conversion? Or maybe you are trying to cast between the wrapper types Float and Long?
If you are trying to do the latter, that is impossible because none of those two classes inherits from the other.
Maybe it would help if you showed us some code showcasing the problem?
Carey Brown wrote:Yep. That should work.
Do you need to print age as well?
Carey Brown wrote:
Carey Brown wrote:You could have been extremely verbose like this:
In your case, because you have a print() method, you could have done this:
However, you COULD NOT have done this without a toString():
Carey Brown wrote:
orry kaplan wrote:That code snip you put above, is that the solution to this? because even though i get that it cannot be printed unless it is toString, is there a way to print this list in a readable format without it as it seems a little confusing to use all the time.
Using toString() is considered the proper way to do it. You could have been extremely verbose like this:
This is not the path usually chosen because of the risks of errors being introduced during maintenance and modification. Also, toString() is quite often used during the debugging stage of development.
Note that inserting parenthesis, commas, field names (e.g. "Name:") into the String are all up to the designer. Whatever you need for meaningful output.