You guys have been a lot of help so far. I've had to create a program that has an array of 5 items, where one side is an item, the other is a price of that item. I've created everything, but I'm struggling to format the price as a decimal. I have 2 classes written. I am posting the class that has my variables assigned. I think that's where I need to do it. I successfully did this last week with an array where the user entered 5 values, but as each array has 2 values, the method I learned last week formatter.format(x); isn't working. printf wouldn't work either. Anyways, here is my code. I need Price to have a set 2 decimal places on output.
My array looks like: ItemArray[0] = new Item("Donut", 1.50);
I'm thinknig maybe instead of a double for price I should use a string, but that is only a work around and not a real solution. It'd work for this program, but it doesn't teach me anything.
... the method I learned last week formatter.format(x); isn't working. printf wouldn't work either.
The code you posted isn't trying to format the output at all, so it's not surprising it doesn't work properly.
If you post up the code you tried to do the formatting, we might be able to suggest where it's going wrong.
Having said that, when I use formatter.format(this.getPrice()) in the toString() method of your Item class, toString() returns a String like this:
Donut $1.50
This seems to be what you wanted, so I can't really see what the problem is.
Testing can show the presence of errors, but not their absence...
E. Dijkstra
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
?? price is a double, it doesn't have a constructor.
It works as intended this way.
You haven't posted the new code, so I'll have to take your word for it.
It is not knowledge, but the act of learning, not possession, but the act of getting there which generates the greatest satisfaction...
F. Gauss
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
Bookmarks