12 Replies - 716 Views - Last Post: 28 January 2012 - 07:52 AM Rate Topic: -----

#1 CS1632012   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 27-January 12

toString()

Posted 27 January 2012 - 10:53 PM

Hi all,

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.
Is This A Good Question/Topic? 0
  • +

Replies To: toString()

#2 moy2414   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 18
  • Joined: 25-January 12

Re: toString()

Posted 27 January 2012 - 11:08 PM

Can you post your code? That way we can see the data and identify what the parameters would be for the method.
Was This Post Helpful? 0
  • +
  • -

#3 CS1632012   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 27-January 12

Re: toString()

Posted 27 January 2012 - 11:26 PM

I'm tring to set up a constructor where the user puts in a date (mm/dd/yyy) and if it is not in the correct format,
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!

Was This Post Helpful? 0
  • +
  • -

#4 JavaIsForever   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-January 12

Re: toString()

Posted 27 January 2012 - 11:31 PM

Just put a .toString(); at the end of your date, and it should work.

try
Date convertedDate = dateFormat.parse(date).toString();

Was This Post Helpful? 0
  • +
  • -

#5 CS1632012   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 27-January 12

Re: toString()

Posted 27 January 2012 - 11:44 PM

Thanks JavaisForever for your reply.

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!
Was This Post Helpful? 0
  • +
  • -

#6 moy2414   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 18
  • Joined: 25-January 12

Re: toString()

Posted 27 January 2012 - 11:57 PM

Can you just make a global ( I doubt thats the proper term but it will have to do) variable outside of the constructers and methods? Create a variable and set it equal to what you want to display. Then you can keep the toString method you put in the last comment and it should return the value because the variable you are sending is not a local one.
Was This Post Helpful? 0
  • +
  • -

#7 JavaIsForever   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-January 12

Re: toString()

Posted 28 January 2012 - 12:10 AM

You don't need to put anything in the toString() method, it's already integrated into the java.util.*(?) import. All you need to do is put it on the end of what you need converted, as long as it's not an integer, where as you'd need
.Integer.toString();

Was This Post Helpful? 0
  • +
  • -

#8 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: toString()

Posted 28 January 2012 - 12:15 AM

Since the constructor is a special method called when the instance of a new object is created, any variables created inside the constructor will be local to that method. So, as moy2414 suggested, use an instance variable for convertedDate so that it is available throughout the scope of the current instance:

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.
Was This Post Helpful? 1
  • +
  • -

#9 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: toString()

Posted 28 January 2012 - 12:22 AM

View PostJavaIsForever, on 28 January 2012 - 02:10 AM, said:

You don't need to put anything in the toString() method, it's already integrated into the java.util.*(?) import. All you need to do is put it on the end of what you need converted, as long as it's not an integer, where as you'd need
.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

Was This Post Helpful? 1
  • +
  • -

#10 CS1632012   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 27-January 12

Re: toString()

Posted 28 January 2012 - 07:31 AM

Thanks for all your replies!

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. =/
Was This Post Helpful? 0
  • +
  • -

#11 Mylo   User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: toString()

Posted 28 January 2012 - 07:33 AM

The toString() method is used to describe an object

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.
Was This Post Helpful? 0
  • +
  • -

#12 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: toString()

Posted 28 January 2012 - 07:37 AM

View PostCS1632012, on 28 January 2012 - 09:31 AM, said:

Thanks for all your replies!

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.
Was This Post Helpful? 1
  • +
  • -

#13 CS1632012   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 27-January 12

Re: toString()

Posted 28 January 2012 - 07:52 AM

Thanks for everyone's input.

Thanks Mylo for helping me to get started on this thread. =)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1