1,105,288 Community Members

Formatting the toString() method

Member Avatar
Newbie Poster
17 posts since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]

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;
Member Avatar
... trying to help
10,364 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]

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

Question Answered as of 3 Years Ago by JamesCherrill
This question has already been solved: Start a new discussion instead
Start New Discussion
Tags Related to this Article