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;

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

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