user2713650
user2713650

Reputation: 99

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[4];
        Scanner input = new Scanner(System.in);
        System.out.println("Enter four integers: ");
        for(int i = 0; i < list.length; i++)
        list[i] = input.nextInt();

        System.out.println();

        barChartPrinter(list);
    }       

    public static void barChartPrinter(int[] numbers) {
        int max = numbers[0];
        for (int i = 0; i < numbers.length; i++)
        if(numbers[i] > max)
            max = numbers[i];
    }
}

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, 4) =

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), then another column of XX (but this time three of these on top of each other, to represent the number 3) and finally another column of XX * 4 vertically.

Any pointers?

Upvotes: 1

Views: 6017

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285415

OK, by now I'm sure that you know that it would be trivially easy to create horizontal bar graphs:

2 6 4

**
******
****

Since the print and println methods of the console's PrintStream (the out of System.out) prints things horizontally. The trick for your assignment is how to print this graph vertically. And for that you're going to have to use a little logic. Since this is obviously homework, I'm not going to give you a solution but rather will suggest options.

  • You know that you'll need to use a loop of some sort to print lines in a row.
  • One possible solution has you figuring out how many times to loop in advance of the loop by first finding the maximal value held by the array. If you do this, then use a for loop.
  • Another possible solution you don't find max, but rather just loop til done. Here use a while loop. This would be the option I'd use, and I'd use a boolean variable, say named keepGoing to help tell the while loop when to keep looping and when to stop.
  • If you go with this latter option, you'll use an int counter in the loop to check what row you're on, and you'll advance this counter inside of the loop.
  • You will need to nest a for loop inside of your while loop to go through each array item.
  • You'd use this counter and the array items (in the for loop within the while loop) to see if the String that you will eventually print should have an "* " added to it.
  • After the inner for loop, you'll print the String you've created.
  • If no "* " are present (you could call myString.trim().isEmpty()), then keepGoing = false; and the while loop should stop.

Edit: your posted code is a bit off in that it tells the user to enter four numbers but only accepts three. You'll want to fix this.


Edit 2 You state:

Hey, sorry yeah I've sorted the code out. I've coded the way to find the max value within the method. I'm stuck on the step of actually producing the chart now. I know that it will print the XX's each row horizontally, but I'm still stumped as to how I can achieve this- I know it will involve a for loop, but I can't see how I can print the XX's for each column; apologies if I'm missing something obvious

Again, break up your problem by splitting up the big problem into little steps, the smallest you can think of, and then solve each small step. You know that you're going to have to construct a String to be printed, and so you should focus on that, on how to construct this String so that an asterisk is present where need be, and a space is present when the column should show no asterisk. Try to come up with a solution, even a partial solution and post it as an edit to your question, much like how I'm posting this edit to my answer.

Upvotes: 2

Related Questions