Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Using 1D Array to print out bar chart vertically

807589Sep 25 2008 — edited Sep 26 2008
Hi all, I'm new here so please correct me if i'm doing things wrong. Anyway, I have an assignment and one of the questions is killing my brain cells.

Ok I need to write a Java method that accepts an array of non-negative integers and displays the corresponding vertical bar chart on the screen.

For example, if array = {8, 7, 6, 5, 4, 3, 2, 1}, then the method should display the chart as shown in Figure 1. You may also assume that the input data have values between 1 and 9.

Output would just be the stars:

8| *
7| * *
6| * * *
5| * * * *
4| * * * * *
3| * * * * * *
2| * * * * * * *
1| * * * * * * * *
-----------------------------------------------
[0] [1] [2] [3] [4] [5] [6] [7]
Figure 1

Note: I chose these numbers as these would make my diagram look legible as the textbox doesn't allow me to leave spaces between *. But I hope the diagram gives you a better picture of what I'm supposed to do.

My initial flow of thought was to read in all values and compare between the first index of the array to the next index(i+1), but I would hit an ArrayIndexOutOfBounds error whenever I hit the tail end of the array. E.g if array were to be of size array[4], I would hit an error when i try to compare array[3] with array [4].

My second thought was to draw up a square whereby the base would be the size of the array and the height being the largest integer. However, it would lead be back to my first problem as I would also have to decide when to print blanks and when to print *.

Any help on this is greatly appreciated. I'm using jGrasp and its basically a command promt kinda output which i'm looking for.

My code so far(short of an additional for loop):
import java.util.*;

public class P4{

static final int MAX = 4;	
	
	public static void main(String[] args)
	{
		int[] data = new int[MAX];
		Scanner sc = new Scanner (System.in);
		System.out.print("Enter 4 inputs: ");
		for (int i=0; i < MAX; i++)
			data[i] = sc.nextInt();
		displayChart(data);
	
	}
	
	public static void displayChart(int[ ] array)
	{
		for(int i=0; i<array.length; i++)
		{
			
			if(array<array[i+1])
System.out.print("O");
else
System.out.print("*");
}

}

}
Edited by: invaz on Sep 25, 2008 12:16 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 24 2008
Added on Sep 25 2008
3 comments
1,020 views