Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: problems with my toString method

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default problems with my toString method

    Here is my constructor that the toString method pulls from:
    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:
    @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!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: problems with my toString method

    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?

    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:
      int first = 123;       //  give variables values
      String secStr = "asdf";
     
      SomeClass sc = new SomeClass(first, secStr); // use variables in a constructor

    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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. toString method
    By pelane in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 23rd, 2012, 01:42 AM
  2. toString method
    By feldmanb700 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 17th, 2011, 09:20 PM
  3. Help with toString method and an addObject method?
    By Camisado in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 12th, 2011, 07:00 AM
  4. Valid toString method?
    By dcshoecousa in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 4th, 2010, 11:55 AM
  5. [SOLVED] toString() method
    By chronoz13 in forum Object Oriented Programming
    Replies: 12
    Last Post: January 19th, 2010, 06:44 AM