problems with my toString method
Here is my constructor that the toString method pulls from:
Code :
public Actor(String sName, int nStrength, int nSpeed, int nHealthPoints){
this.sName = sName;
this.nStrength = nStrength;
this.nSpeed = nSpeed;
this.nHealthPoints = nHealthPoints;
++nSequenceCount;
}
Here is my toString method:
Code :
@Override
public String toString(){
return String.format("Name:%-12s Strength:%2d Health:%2d Speed:%2d", sName, nStrength, nHealthPoints, nSpeed);
My problem is that the values for the four variables are unknown until the program is run. They are user input variable, or randomly generated depending on what the user chooses. How do I build a new object in main if I don't know what the variables will be?
Actor a1 = new Actor()
*I don't know what to put in the brackets
I hope that makes sense. Is there another way to use the toString method? It's our first time using it in class, so I'm still learning.
Thanks for any help!
Re: problems with my toString method
Quote:
values for the four variables are unknown until the program is run.
That's often true for most variables used in a program.
Why is that a problem for you with your program?
Quote:
what to put in the brackets
You should define variables that will be given values as the program executes. When they all have values, you can then use the variables as arguments to the constructor:
Code :
int first = 123; // give variables values
String secStr = "asdf";
SomeClass sc = new SomeClass(first, secStr); // use variables in a constructor
Quote:
another way to use the toString method
The toString() method needs to return a String. It's up to the programmer to decide what goes in the String and how to build it.