Search code examples
javaarraysloopsmethodsnumberformatter

Trying to pass 2 arrays to a method and print out a table using values in the 2 arrays


I'm trying to create 2 arrays in my main method with hard-coded values, then pass them down into my generateReport method and print out a table.

The question I'm working with is

With my code below I'm getting errors in my main and the section of code that is commented is supposed to display the currency which is default on the computer you're working on.

public class sheet13t5
{
    public static void main(String[] args)
    {
        int[] units = new int[] {10, 1, 6, 2, 7};
        double[] prices = new double[] {25.99, 30.49, 12.00, 15.55, 9.55};
        generateReport(units[], prices[]);
    }

    public static void generateReport(int units[], double prices[])
    {
        System.out.println("Event #\tTicket Price\tTickets Sold\tTotal Sales Value\tHistogram (+ for each ticket sold)");
        NumberFormatter format = NumberFormat.getCurrencyInstance();
        for (int i = 0; i < units.length; i++)
        {
            System.out.print(i++ + "\t");
            System.out.printf("%7d\t", ++i);
            System.out.print(prices[i] + "\t" + units[i]);
            //System.out.printf(format.
            System.out.print(prices[i] * units[i]);
            for (int j = 0; j < units.length; j++)
                System.out.print("+");

            System.out.println();
        }
    }
}

Solution

  • Your method call should be

    generateReport(units,prices);
    

    instead of

    generateReport(units[], prices[]);