toString()
Page 1 of 112 Replies - 716 Views - Last Post: 28 January 2012 - 07:52 AM
#1
toString()
Posted 27 January 2012 - 10:53 PM
I am trying to put a toString method in my program. It needs to be in the format of mm/dd/yyyy. I'm not sure where to begin looking....any suggestions would be appreciated.
Replies To: toString()
#2
Re: toString()
Posted 27 January 2012 - 11:08 PM
#3
Re: toString()
Posted 27 January 2012 - 11:26 PM
java has to gets the error. My code is not working for some reason. For printing purposes, toString() method should return a string with the date in mm/dd/yyyy format. Thanks for helping me out.
public SimpleDate (String date) {
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date convertedDate = dateFormat.parse(date);
}
catch(ParseException pe) {
System.out.println("ERROR: could not parse date in string \"" +
date + "\"");
}
}
public String toString()
This post has been edited by jon.kiparsky: 27 January 2012 - 11:27 PM
Reason for edit:: added code tags - please use them in the future!
#4
Re: toString()
Posted 27 January 2012 - 11:31 PM
try
Date convertedDate = dateFormat.parse(date).toString();
#5
Re: toString()
Posted 27 January 2012 - 11:44 PM
I'm not if I understand what you mean.
I'm trying to create a toString() and not sure what I should put in the body of the method. So it is formatted in the mm/dd/yyyy.
I'm thought of putting
public String toString(){
return convertedDate;
Does not work since convertedDate is a local reference? Also, does my constructor look ok?
Thanks!
#6
Re: toString()
Posted 27 January 2012 - 11:57 PM
#7
Re: toString()
Posted 28 January 2012 - 12:10 AM
.Integer.toString();
#8
Re: toString()
Posted 28 January 2012 - 12:15 AM
class SimpleDate
{
Date convertedDate;
// constructor
public SimpleDate( String date )
{
// define convertedDate using date for this instance of SimpleDate
}
}
Then, your toString() method can simply return convertedDate.
#9
Re: toString()
Posted 28 January 2012 - 12:22 AM
JavaIsForever, on 28 January 2012 - 02:10 AM, said:
.Integer.toString();
In Java, every class inherits Java's Object class, and Object class has a toString() method. To 'customize' a class' toString() behavior, a class, like the SimpleDate class, can override the Object class' toString() method with its own toString() method.
Edit: I should have added (before the forum crashed):
The toString() method returns a String when called explicitly as any other method, classInstance.toString(), but it is also called implicitly when the situation requires a String, as in:
System.out.println( classInstance );
In the implicit case, overridng toString() is especially useful for formatting an instance of the class for output to the user in a useful way.
This post has been edited by GregBrannon: 28 January 2012 - 03:50 AM
#10
Re: toString()
Posted 28 January 2012 - 07:31 AM
Greg, I did that and I got a type mismatch. I understand what you mean by putting it as a field variable but my method needs to return a String object instead of a Date object. =/
#11
Re: toString()
Posted 28 January 2012 - 07:33 AM
So to return the date object string, you do:
public String toString()
{
return myDateObject.toString();
}
If it helps, you should look at other classes, and what they return. For example, the Color class says something like "[r=200,g=200,b=200]" which can be helpful as it desribes the color.
#12
Re: toString()
Posted 28 January 2012 - 07:37 AM
CS1632012, on 28 January 2012 - 09:31 AM, said:
Greg, I did that and I got a type mismatch. I understand what you mean by putting it as a field variable but my method needs to return a String object instead of a Date object. =/
I didn't provide the code for creating the String from the Date object in your toString() method. That was your job.
#13
Re: toString()
Posted 28 January 2012 - 07:52 AM
Thanks Mylo for helping me to get started on this thread. =)

New Topic/Question
Reply


MultiQuote



|