We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,434 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Formatting the toString() method

I was writing a program and I wasn't sure sure how to do letter b. I have the methods and constructor but I'm not sure how to format the float.

2. Create a class called Item.
a. Item will need instance variables itemID, itemName, inStore, and
Price (of types String, String, int and double accordingly).
b. Item will need appropriate methods and constructors. Make sure to have a
toString() method that prints the item, making sure to format the float to 2
decimal places with $.

This is what I have for the toString() method so far.

public String toString()
    {
        return itemID + ": " + itemName + ", " + inStore + ", " + Price;
        
    }
2
Contributors
1
Reply
1 Hour
Discussion Span
2 Years Ago
Last Updated
2
Views
Question
Answered
Na'Vi
Newbie Poster
17 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

You can use the format method in the String class to convert stuff to String using printf-style formatting codes.

JamesCherrill
... trying to help
Moderator
8,668 posts since Apr 2008
Reputation Points: 2,636
Solved Threads: 1,479
Skill Endorsements: 33
Question Answered as of 2 Years Ago by JamesCherrill

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0604 seconds using 2.71MB