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
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!
Re: Can someone help me understand toString()?
Quote:
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.
Quote:
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:
Code :
int x=5;
String y = Integer.toString(x);
System.out.println(y);
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!
Re: Can someone help me understand toString()?
Quote:
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?
Re: Can someone help me understand toString()?
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:
Code :
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:
Code :
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.
Re: Can someone help me understand toString()?
Quote:
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.
Re: Can someone help me understand toString()?
Quote:
Originally Posted by
Psychotron
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:
Code :
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:
Code :
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.
Re: Can someone help me understand toString()?
Quote:
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.
Re: Can someone help me understand toString()?
Quote:
Originally Posted by
Psychotron
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:
Code :
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.