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

Thread: Can someone help me understand toString()?

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Can someone help me understand toString()?

    Hello. I need to know how toString() works. Can someone show an example of how toString() is manipulating data and how to get it to do it? Like maybe an example of a program with toString() and the same one without toString() to see how it works.

    I have very basic things in my program, almost no content. My professor just wants us to demonstrate that we know how to use toString(). I've looked all over the net and in my book, and no one can explain toString() in a way that I can understand.

    Thank you,
    Brad


  2. #2
    Member
    Join Date
    Dec 2011
    Location
    United States
    Posts
    94
    My Mood
    Amused
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default Re: Can someone help me understand toString()?

    You seriously saying that you have looked all over the internet for a use but have not found one? Have you looked at the Java docs on Oracle's website? Basically, when you want to return something in a method, you can use a toString method to make it easy to read. It is like converting things like int + string into a single string and printing it out. If you still do not understand it, go online again, use java doc. Check the String class and its methods - you will find it there! Note: You are not supposed to come to this forum and ask people to do your homework for you!

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Can someone help me understand toString()?

    Basically, when you want to return something in a method, you can use a toString method to make it easy to read.
    elisha.java clear it.
    OP: toString() is a method provided by java.
    public String toString()

    This object (which is already a string!) is itself returned.
    It can never convert int but the wrapper Integer to String and is mostly helpful when you want to use your data as String rather than some other primitive or non-primitive type.
    Example:
            int x=5;
            String y = Integer.toString(x);
            System.out.println(y);

  4. #4
    Member
    Join Date
    Dec 2011
    Location
    United States
    Posts
    94
    My Mood
    Amused
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default Re: Can someone help me understand toString()?

    If you are confused by all the explanations check this out:
    Java Platform SE 7
    Let me fix what I said earlier as pointed out by Mr.777. If you have a method that returns a String, then you can use the toString method to do so. Anyway, Check out that link and hope it will help you!

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Can someone help me understand toString()?

    If you have a method that returns a String, then you can use the toString method to do so.
    I can't understand what do you mean by using toString method to convert into String? Why not simply using String?

  6. #6
    Member
    Join Date
    Dec 2011
    Location
    United States
    Posts
    94
    My Mood
    Amused
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default Re: Can someone help me understand toString()?


  7. #7
    Junior Member
    Join Date
    May 2011
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Can someone help me understand toString()?

    I'm trying to understand a concept not asking you to do my homework. I'd ask my tutor if I could, but she's not available. Yes, as stupid as you may make me feel, I've seriously looked all over. Don't get mad at me, I'm very new to Java.

    I tried what I think you are saying, Mr.777:
    int b=1;
            int x=5;
            String y = Integer.toString(x);
            System.out.println(y)

    When I do the code below I can't get reach it outside the method. "System.out.println(s);" doesn't work. How do I get it to work outside the method:

    public String toString()
    {
    String s = "";
     
    s = s + "ID: " + name;
    s = s + "ID: " + grade;
     
    return s;
    }

    Also, what does the return statement do here? I know what it does with other methods, like int, but how do I incorporate my code using it? How do I use the toString() method to work outside the method?

    Thanks.

  8. #8
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Can someone help me understand toString()?

    what does the return statement do here?
    Returns the value of s.

    And declare s outside the function if you want to access s else you are returning value of s from the function, so simply get this value and print out by calling the function.

    Word to Wise: I will recommend you to read the very basics of java because we appreciate you are in very learning phase and the point made by elisha.java was quiet right. You must learn the basics, what is the scope and lifetime of variables. How to define functions, in order to understand this concept.

    Good Luck.
    NOTE: Also, toString() is builtin method of java.lang.String so you can not override it as you did here.

  9. #9
    Member
    Join Date
    Dec 2011
    Location
    United States
    Posts
    94
    My Mood
    Amused
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default Re: Can someone help me understand toString()?

    Quote Originally Posted by Psychotron View Post
    I'm trying to understand a concept not asking you to do my homework. I'd ask my tutor if I could, but she's not available. Yes, as stupid as you may make me feel, I've seriously looked all over. Don't get mad at me, I'm very new to Java.

    I tried what I think you are saying, Mr.777:
    int b=1;
            int x=5;
            String y = Integer.toString(x);
            System.out.println(y)

    When I do the code below I can't get reach it outside the method. "System.out.println(s);" doesn't work. How do I get it to work outside the method:

    public String toString()
    {
    String s = "";
     
    s = s + "ID: " + name;
    s = s + "ID: " + grade;
     
    return s;
    }

    Also, what does the return statement do here? I know what it does with other methods, like int, but how do I incorporate my code using it? How do I use the toString() method to work outside the method?

    Thanks.
    If you took that advice too personal, I am sorry. I was just trying to help. We are all learning after all! Thank you.

  10. #10
    Junior Member
    Join Date
    May 2011
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Can someone help me understand toString()?

    appreciate you are in very learning phase and the point made by elisha.java was quiet right. You must learn the basics
    well ok. I'm even trying to wrap my mind around "Java Platform SE 7" you just had me look at and could use some help understanding it as well, but no one's obligated to help. You may think I "can't be serious" when I ask, but what's easy for you is very difficult for me. What can I say, I'm having trouble with Java. I don't see what's wrong with that, and I don't see why someone with my questions should have no place here.

  11. The Following User Says Thank You to Psychotron For This Useful Post:

    elisha.java (January 18th, 2012)

  12. #11
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Can someone help me understand toString()?

    Quote Originally Posted by Psychotron View Post
    When I do the code below I can't get reach it outside the method. "System.out.println(s);" doesn't work. How do I get it to work outside the method:

    public String toString()
    {
    String s = "";
     
    s = s + "ID: " + name;
    s = s + "ID: " + grade;
     
    return s;
    }
    Could you include the code you're using to call the toString() method in context? And clarify what you mean by "doesn't work" (is there an compile error? If so, please post it). The more detail you provide will not only help us help you solve the problem, but may even help you solve the problem yourself.

Similar Threads

  1. Why cant I use toString() with vector?
    By tarkal in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 14th, 2011, 08:24 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. [SOLVED] How to choose a different toString() Besides the default one?
    By Hallowed in forum Java Theory & Questions
    Replies: 12
    Last Post: April 8th, 2011, 03:05 PM
  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