Results 1 to 8 of 8
- 12-13-2010, 01:11 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 7
- Rep Power
- 0
Why isn't my toString method working
I created toString method in another class but when I call it, it's not working and its giving me an error mesg.Java Code:public void printCapacity() { System.out.println("The list capcity is " + (capacity).toString()); }
When I write my code like this:
When I run it, it prints weird. (if that makes any sense) I'm new at this so forgive me for my stupidity.Java Code:public void printCapacity() { System.out.println("The list capcity is " + (capacity) + toString()); }
Arri
-
Why the parenthesis around capacity? If the variable is called capacity, then you simply call toString() on the variable. But this being said, you don't even need to call toString from within a println method call since it will automatically be called. I think you need to tell us more.
- 12-13-2010, 01:36 AM #3
Member
- Join Date
- Dec 2010
- Posts
- 7
- Rep Power
- 0
This program involves use of arrays. My professor wants us to incorporate the toString method with this program. But I didn't see the need for it because it prints perfectly without it, and truthfully I just didn't know how to do it. He grades hard and if in the requirements, it has to be there. And I don't know why I put the parens around capacity. I was following a example in my book. Hope that helps
Arri
- 12-13-2010, 02:14 AM #4
Member
- Join Date
- Nov 2010
- Posts
- 73
- Rep Power
- 0
The following two examples does exactly the same:
Java Code:System.out.println("The capacity is: " + capacity);So basically what I think Fubarable is saying is that we need to know exactly how the toString() is coded.Java Code:System.out.println("The capacity is: " + capacity.toString());
- 12-13-2010, 04:48 AM #5
Member
- Join Date
- Dec 2010
- Posts
- 7
- Rep Power
- 0
Ok I see.
This works and prints fine:Java Code:public String toString() { return name + "\n" + description + "\n" + health + "\n" + strength; }
Java Code:public String displayAsString() { return "----------------------------------\n" + name + "\n" + description + "\nHealth: " + health + " Strength: " + strength + "\n----------------------------------\n"; }ArriJava Code:System.out.println("Max is found at index = " + x + "\n" + list[x].displayAsString()); return x; }
- 12-13-2010, 04:50 AM #6
Member
- Join Date
- Dec 2010
- Posts
- 7
- Rep Power
- 0
It gives me an error.Java Code:System.out.println("Max is found at index = " + (x).toString() + "\n" + list[x].displayAsString()); return x; }
- 12-13-2010, 04:53 AM #7
Member
- Join Date
- Nov 2010
- Posts
- 73
- Rep Power
- 0
You said you got an error message in the first call of the toString(), is it a syntax-error? - or do you mean an exception? (runtime-error).
In any case, can you post it?
EDIT: I'm only guessing here but isn't "x" an 'int'? As far as I'm aware, toString()-methods cannot be called on primitive types (all the types with only lower case letters; int, float, boolean etc. while e.g. String isn't one) and your toString()-method should be located in the class, that you're variable "capacity" (or whatever you're trying to print) is an object of.
All classes that extends Object (they do that by default) has a toString()-method by default although it may look weird to the user - so writing one yourself overrides that.Last edited by Muskar; 12-13-2010 at 05:04 AM.
- 12-13-2010, 05:34 AM #8
Senior Member
- Join Date
- Oct 2009
- Location
- California,US
- Posts
- 201
- Rep Power
- 4
Similar Threads
-
overriding toString method
By matin1234 in forum New To JavaReplies: 3Last Post: 06-01-2010, 04:35 AM -
toString() method error?
By blueduiker in forum New To JavaReplies: 6Last Post: 02-07-2010, 03:19 AM -
Trying to write toString method
By curious in forum New To JavaReplies: 3Last Post: 10-28-2009, 02:43 AM -
toString() method
By 01allenh in forum New To JavaReplies: 2Last Post: 03-25-2009, 11:43 PM -
toString method
By apfroggy0408 in forum New To JavaReplies: 6Last Post: 01-31-2008, 04:08 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks