Need help formatting data
for (int counter = 0; counter < array.length; counter++ )
System.out.printf("%02d%10s%20d%,20.2d%,20.2d\n", counter, title, stock, price, value );
"counter" and "stock" are integers. "Title" is a string. "Price" and "Value" are both doubles, and I want them to print out in the form 0.00. Any ideas?
[415 byte] By [
ksova] at [2007-11-27 1:25:31]

> "Price" and "Value" are both doubles, and I want them to print out in > the form 0.00. Any ideas? Shouldn't you use 'f' instead of 'd' then?
I had f originally, but it wasn't working either. That's why I tried using d instead.
ksova at 2007-7-12 0:18:30 >

Incidentally, I changed it back to f, and got rid of the commas, but I'm still getting the same errors. Here's the code for the entire program:
public class Inventory1
{
private Dvd inventory[];
private String title[];
private int stock[];
private double price[];
private double value[];
public void Inventory1()
{
final int ARRAY_LENGTH = 5;
int array[] = new int[ ARRAY_LENGTH ];
String title[] = { "Garbage", "Generic", "Cheeseland", "Elbow", "Something" };
int stock[] = { 13, 22, 11, 17, 93 };
double price[] = { 10.99, 11.49, 13.99, 11.49, 10.00 };
double value[] = { 142.87, 252.78, 153.89, 195.33, 930.00 };
System.out.printf( "%s%10s%20s%15s%15s\n", "Item Number", "Title", "Units In Stock", "Unit Price", "Total Value" );
for ( int counter = 0; counter < array.length; ++counter )
System.out.printf( "%02d%10s%20d%15.2f%15.2f\n", counter, title, stock, price, value );
}
}
It outputs the Header line fine, then it runs into the errors.
ksova at 2007-7-12 0:18:30 >

title, stock, price and value are [url= http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html]arrays[/url] (not int, String or double)
Ahhh...right. Is there a different format specifier for arrays? Is this closer to being correct:
for ( int counter = 0; counter < array.length; ++counter )
System.out.printf( "%02d%10s%20d%d%15d\n", counter, title[ counter ], stock[ counter ], price[ counter ], value[ counter ] );
ksova at 2007-7-12 0:18:30 >

More info:The problem is apparently occurring at the specifier for "stock" because it prints out the first item number and title before it hits the errors.
ksova at 2007-7-12 0:18:30 >

> Is this closer to being correct
You can tell. (But why did you switched back to d for floating point values ?)
By the way, using four different arrays for representing a list of coherent items is definitely not the way to go.
What about creating a class representing an item, and handle instances of that class (e.g. in a List) ?public class Item {
private String title;
private int stock;
private double price; // btw, not advisable to use double for prices
public Item(String title, int stock, double price) {
this.title = title;
this.stock = stock;
this.price = price; // btw, not advisable to use double for prices
}
public String getTitle() {
return title;
}
public int getStock() {
return stock;
}
public double getPrice() {
return price;
}
public double getStockValue() {
return price*stock;
}
/* add methods to allow modifying values, e.g. stock and price */
public String toString() {
return String.format("%10s%20d%15.2f", title, stock, price);
}
}
Alright, it's working now.Thanks for the input, Tim. Consider me enchanted.
ksova at 2007-7-12 0:18:30 >
