Saturday, 24 August 2013

Java bar chart method

Java bar chart method

I need to print out a bar chart, via a call from a method barChartPrinter.
I.e. barChartPrinter(5, 6, 2); would print: a vertical column of 5 XX's
followed by a space then a vertical column of 6 XX's and a vertical column
of 2 XX's.
The numbers are based on user input, so I'm thinking I need to gather the
numbers and store those in an array. And I'm thinking a for loop will be
involved, but I'm not sure how I'd do it.
public class BarChart {
public static void main(String args[]) {
int[] list = new int[3];
Scanner input = new Scanner(System.in);
System.out.println("Enter four integers: ");
for(int i = 0; i < list.length; i++)
list[i] = input.nextInt();
barChartPrinter(list);
}
public static void barChartPrinter(int[] numbers) {
}
}
This is where I get stuck: defining the method that will print out the bar
chart; given the user input values. The method needs to print out in this
format, i.e.
barChartPrinter(1, 2, 3) =
For some reason, it won't display how the bar chart should be constructed
on here so I'll just describe it:
There would first be a column of XX, then a column of XX (two of these
vertically to represent 2) and then another column of XX (but this time
three of these on top of each other, to represent the number 3).
Any pointers?
Thanks!

No comments:

Post a Comment