I just can't get the numbers with decimals to display and I am trying to display it with a 2 dimension array. Is it possible to do so?

import java.util.ArrayList;

public interface VendingMachineDriver
{
    public static void main(String[] args)
    {
        double [][] itemPQ =
        {
            {1.25, 20}, //Lays Chips
            {1.25, 20}, //Sun Chips
            {1.25, 15}, //Cheetos Chips
            {1.25, 15}, //Fritos Chips
            {1.25, 20}, //Doritos Chips
            {1.75, 16}, //Popcorn
            {1.75, 20}, //Ruffles
            {1.75, 14}, //Cookie
            {1.50, 9}, //Reese's
            {1.50, 15}, //KitKat
            {1.50, 20}, //Snickers
            {1.50, 20}, //m&m's
            {2.00, 20}, //Donuts
            {2.00, 20}, //Pretzels
            {2.00, 10}, //Cheez-it
            {2.00, 15} //Chex Mix
        };
        String[] itemNumbers = {"A1", "A2", "A3", "A4", "B1", "B2", "B3", "B4", "C1", "C2", "C3", "C4", "D1", "D2", "D3", "D4"};
        String[] itemDisplay = {"Price", "Quantity"};
        VendingItem vi = new VendingItem(itemPQ, itemNumbers, itemDisplay);

        vi.displayItem();
    }
}

This is the second class with the methods. The array displayed when I made the itemPQ into an int, but when I make it a double it seems to stop working.

import java.util.Scanner;

public class VendingItem
{
    private double[][] itemPQ;
    private String[] itemNumbers;
    private String[] itemDisplay;
    String item;
    double quantity;
    double cost;
    double cost1;
    double quantity1;
    public VendingItem(double[][] pq, String[] n, String[] d)
    {
        itemPQ = pq;
        itemNumbers = n;
        itemDisplay = d;
    }

    //************************************************************************************************

    public VendingItem setItem(String item, double quantity, double cost)
    {
        this.item = item;
        this.quantity = quantity;
        this.cost = cost;
        return this;
    }

    //************************************************************************************************

    public void displayItem()
    {
        final String NUM_FMT_STR = "%5s";
        final String IPQ_FMT_STR = "%5d";
        final String DISP_FMT_STR = "%5s";
        System.out.printf(DISP_FMT_STR, "");
        for (int col=0; col<itemDisplay.length; col++)
        {
            System.out.printf(DISP_FMT_STR, itemDisplay[col]);
        }
        System.out.println();
        for (int row=0; row<itemPQ.length; row++)
        {
            System.out.printf(NUM_FMT_STR, itemNumbers[row]);
            for (int col=0; col<itemPQ[0].length; col++)
            {
            System.out.printf(IPQ_FMT_STR, itemPQ[row][col]);
            }
            System.out.println();
        }
    }
}
3

There are 3 answers

1
FaceFTW On

Assuming that the array is itemPQ, and the number you are trying to print is at itemPQ[x][y],he best way is either use Double.toString(itemPQ[x][y]) or make an explicit cast by using this code

String output = (String) itemPQ[x][y]
System.out.print(output);

The formatting code you seem to be using is unnecessary, as the values shown in the original are precise by two decimal places maximum.

Because a double extends Object, this means it inherits the toString() method

3
Mrs.Brightside On

It worked for me when I changed this

 final String IPQ_FMT_STR = "%5d";

to this

 final String IPQ_FMT_STR = "%5s";

This was the conversion exception you were getting

0
Adrian Shum On

Not clear on your question. You mean System.out.printf("%5d", num); works if num is int, but not when num is double right?

It is simply because %d is for formatting integer. You will receive java.util.IllegalFormatConversionException if you use it to format a floating number. To format floating point number, use %f instead. i.e.

System.out.printf("%5f", aFloatNum);
// or the following, which give similar result as %5d
System.out.printf("%5.0f", aFloatNum);

Refer to Java API Doc for format string usage https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html