Having trouble with a toString() method
I am making a program that keeps up with hot dog stands and sells hotdogs. This program is composed of two files, the first one I got to work fine that has the hot dog stand class in it. Now I am moving on and stuck on a toString()
method where it keeps telling me that it is the illegal start of the expression. I am still pretty new to the toString method and am confused why it would say that. Here is the code
public class TheHotDogStands
{
public static void main(String[]args)
{
HotDogStand one = new HotDogStand ("Bob's hotdog stand" , "0081" , "5");
HotDogStand two = new HotDogStand ("Chris's hotdog stand" , "4591" , "3");
HotDogStand three = new HotDogStand ("Matt's hotdog stand" , "1171" , "10");
public String toString()
{
System.out.println("Total sold =" + (one.getNumSold() + two.getNumSold() + three.getNumSold()) + "\n");
}
on开发者_如何学运维e.SetJustSold();
two.SetJustSold();
three.SetJustSold();
System.out.println(one.getName + one.getID + one.getNumSold);
System.out.println(two.getName + two.getID + two.getNumSold);
System.out.println(three.getName + three.getID + three.getNumSold);
System.out.println("Total sold for all stands = " + (one.getNumSold() + two.getNumSold() + three.getNumSold()));
public one(one aOne)
{
nameHotDogStand = aNameHotDogStand;
IDnumber = aIDnumber;
hotDogsSold = aHotDogsSold;
}
}
Do I need to turn the System.out.println
to "return" instead? Any info on what I did wrong would be appreciated.
Either its a copy paste bug, or you have your ToString function defined INSIDE the main function. You can't do that. Place it outside main()
You are trying to define toString()
in the body of another method. You are not allowed to define one method within another. Same goes for the one()
method.
toString()
is meant to give you a string representation of a given object. The only place it is feasible to define toString()
in your case is in the HotDogStand
class. There toString()
will give information about a particular stand.
Note that toString()
should be used mainly for debug purposes and not for deciding the program flow.
toString() is expecting you to return a String, and it is being declared inside the main() method.
Do I need to turn the System.out.println to "return" instead? << Yes a toString method always returns a String but also, your toString method must be outside of the main method
You are right. the toString() method doesn't print something to the screen, but returns a string representation (as you decide it should be) of the object.
public class TheHotDogStands
{
public static void main(String[]args)
{
HotDogStand one = new HotDogStand ("Bob's hotdog stand" , "0081" , "5");
HotDogStand two = new HotDogStand ("Chris's hotdog stand" , "4591" , "3");
HotDogStand three = new HotDogStand ("Matt's hotdog stand" , "1171" , "10");
one.SetJustSold();
two.SetJustSold();
three.SetJustSold();
System.out.println(one.getName + one.getID + one.getNumSold);
System.out.println(two.getName + two.getID + two.getNumSold);
System.out.println(three.getName + three.getID + three.getNumSold);
System.out.println("Total sold for all stands = " + (one.getNumSold() + two.getNumSold() + three.getNumSold()));
}
public String toString()
{
return "Total sold =" + (one.getNumSold() + two.getNumSold() + three.getNumSold()) + "\n";
}
public one(one aOne)
{
nameHotDogStand = aNameHotDogStand;
IDnumber = aIDnumber;
hotDogsSold = aHotDogsSold;
}
}
This is what you want your file to look like.
As people said:
Don't declare a function inside a function.
toString should return a String, not print it.
Your one function looks strange too. You should capitalize the "one" class, and those aNameHotDogStand, aIDnumber, aHotDogsSold are strange, you might want to get them from the object parameter, but I don't know what you're trying to do so it's hard to tell...
Actually, your toString is also wrong. one, two and three are instantiated inside your main, they are not accessible outside it. You want to declare them inside the class instead (as members) to reuse them elsewhere.
And I'm really wondering what is your one function doing here... What are nameHotDogStand, IDnumber, hotDogsSold? Is this the whole code?
Yes, you declared toString
to return a String
and your returning nothing (that is, you are missing a return
statement.
Obviously, this doesn't forbid you to just print, but it vanifies the purpose of the toString
method, that is return a human readable (and printable) representation of an object.
Also, you are defining a method inside another method.
You are trying to declare new functions (toString, one) inside a function (main)... this isn't allowed. Also one, two and three should also be members of the class and not declared in the main function
精彩评论