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 6 of 6

Thread: Information on toString()

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    14
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Question Information on toString()

    Hi,

    I need to use the toString() to display some of the attributes about a class. However I have int values also that need to be displayed.

    Is there anyway to include these into the following?

     
    public String toString()
       {
          return "This player name is" + playerName;
       }

    Thanks.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,459
    My Mood
    Mellow
    Thanks
    148
    Thanked 607 Times in 594 Posts

    Default Re: Information on toString()

    Have you never added a numeric variable to a String before? An example:

    int myValue = 5
    String newString = "" + myValue;

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    14
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Information on toString()

    No I have not until now. Which I was able to do however once additing it into my toString() method as below

     public String toString()
       {
          return "This player name is" + playerName;
          return "This player costs" + playerValue;
       }

    I now get unreachable statement and I have never come across this before. Any ideas on how to resolve this?

    Thanks.

  4. #4
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Information on toString()

    Quote Originally Posted by jamesh View Post
     public String toString()
       {
          return "This player name is" + playerName;
          return "This player costs" + playerValue;
       }

    I now get unreachable statement and I have never come across this before. Any ideas on how to resolve this?
    Obviously, this doesn't compile. You must return 1 String object. You can build the string in any way you want just using the + operator. If either of operands is a String, it's a "string concatenation".
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  5. The Following User Says Thank You to andbin For This Useful Post:

    jamesh (January 14th, 2014)

  6. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,459
    My Mood
    Mellow
    Thanks
    148
    Thanked 607 Times in 594 Posts

    Default Re: Information on toString()

    The first return leaves the method with the specified String object, so the second return is "unreachable." Put the entire String object in a single return statement. Either build the String object to be returned beforehand (unnecessary but sometimes neater and clearer) or build it in the return statement.

  7. #6
    Junior Member
    Join Date
    Dec 2013
    Posts
    14
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Information on toString()

    Hi Andbin,

    Thanks for that thought looked wrong! Got it working now.

    Thanks again.

    --- Update ---

    Hi Grey,

    Much appreciated, all working now after the change.

    Thanks for pointing me in the right direction.

Similar Threads

  1. Replies: 0
    Last Post: February 1st, 2013, 11:25 AM
  2. Array toString
    By smithmar in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 23rd, 2012, 06:10 PM
  3. Can I use toString() here?
    By titowinky in forum Java Theory & Questions
    Replies: 2
    Last Post: May 30th, 2012, 07:15 AM
  4. toString method
    By pelane in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 23rd, 2012, 01:42 AM
  5. Why cant I use toString() with vector?
    By tarkal in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 14th, 2011, 07:24 AM

Tags for this Thread