RE: interfaces help 11-27-2013, 02:29 PM
#31
@blackeagle:
There are two problems in your toString() method.
The first one is that you can't call a method on a primitive value. Primitives are all types starting with a lowercase letter, like int, double, float. These are no objects, so they have no method. But you try to call the method toString() on an int.
The second problem is using several consecutive return statements. return means: "return that value and leave the method". So your code after the first return is not reachable (will never be executed).
What you really should do is create a single string that contains all of your information. Use the concatentation operator + to build up your string.
Example:
Primitive values like price will be converted to a string automatically.
This code snippet is wrong and the problems above actually came from your example. If you provide snippets to programming beginners, make sure they are working and do not create more problems than helping.
That is nonsense.
There are two problems in your toString() method.
The first one is that you can't call a method on a primitive value. Primitives are all types starting with a lowercase letter, like int, double, float. These are no objects, so they have no method. But you try to call the method toString() on an int.
The second problem is using several consecutive return statements. return means: "return that value and leave the method". So your code after the first return is not reachable (will never be executed).
What you really should do is create a single string that contains all of your information. Use the concatentation operator + to build up your string.
Example:
Code:
int price = 10;
String item = "rubics cube";
return "price: " + price + ", item: " + item;
Primitive values like price will be converted to a string automatically.
(11-27-2013, 02:07 AM)darthjames226 Wrote: The toString() returns a string equivalent. For example:
int value = 30;
System.out.println(value.toString());
This code snippet would print out 30. Notice that value will be displayed as a String and not as an integer.
This code snippet is wrong and the problems above actually came from your example. If you provide snippets to programming beginners, make sure they are working and do not create more problems than helping.
(11-27-2013, 03:36 AM)darthjames226 Wrote: Only thing I can see that could be wrong is this:
@Override
public String toString(){
return name;
return purchasePrice.toString(); //calls your override method
return salePrice.toString(): //uses colon instead of semicolon
//calls your override method
}
You used a colon instead of a semicolon on your last statement. Since you did a override on toString(), you are calling your method on the statements inside of your method.
Don't override toString(). Change its name to something else and try to compile. That will let you know if that's the problem.
That is nonsense.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.
Expressed feelings are just an attempt to simulate humans.
![[Image: 2YpkRjy.png]](http://i.imgur.com/2YpkRjy.png)