# Thread: Display number of characters like bar chart

1. Member Join Date
Oct 2011
Posts
49
Rep Power
0

## Display number of characters like bar chart

I want to write a program where a user inputs a character and 5 numbers between 1 -9, and the program should display the character in a bar-chart-like format. E.g. if user inputs X 2 3 4 1, 5, the result should be:

Java Code:
```        X
X   X
X X   X
X X X   X
X X X X X
2 3 4 1 5```
My idea was to write a matrix using a 2-dimensional array with the number of rows representing the maximum of the given numbers; the number of columns are fixed at 5. And then loop through all elements one by one, filling in the number of characters required and fill the remaining elements with spaces. But I have a problem with the displaying part. I don't know how I can display it in this manner. Any ideas, hints or helps are appreciated and thanks in advance.  Reply With Quote

2. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## Re: Display number of characters like bar chart

I have a problem with the displaying part. I don't know how I can display it in this manner.
If I understand you correctly you would fill your five arrays so they look like:

Java Code:
```XX___
XXX__
XXXX_
X____
XXXXX```
Think about how you would determine what character to print at ? in the following:

Java Code:
```        X
X ? X
X X   X
X X X   X
X X X X X
2 3 4 1 5```
Which array would you check? And which position within that array?

-----

Personally, I wonder whether the array is needed at all. The ? is four elements across, so I would look at the fourth number (1). And it is in the second row, so I would check 4 (=6-2). 4>1, so the ? will be a space - had it been <=1 then it would be an X.

Such a process could be applied five time to generate the five rows of X's and spaces.

Either way, forget writing the code for a bit and concentrate on how you, yourself, would decide in a systematic way what the value of ? should be. Once you have a comprehensive and detailed "recipe" it can form the basis of the code.  Reply With Quote

3. Member Join Date
Oct 2011
Posts
49
Rep Power
0

## Re: Display number of characters like bar chart

Thanks, here is the code now, and it seems to be working, at least with whatever I tested it so far:

Java Code:
```/*Thanks for the tip. I finally managed to solve it using your suggestion
* Though I still felt I needed array to be able to find max etc.
* Have a look at it
* I am sure it is inefficient and there are probably many unnecessary lines
* in the code
*/
public class test {
public static void main (String[] args){
String str = args;
int num1 = 0, num2 = 0, num3 = 0, num4 = 0, num5 = 0;
try {
num1 = Integer.parseInt(args);
num2 = Integer.parseInt(args);
num3 = Integer.parseInt(args);
num4 = Integer.parseInt(args);
num5 = Integer.parseInt(args);
}
/*Here I have a question, can you use many arguments in a catch block
* that is, can you use it to check many errors
*/
catch (NumberFormatException nfe) {
System.out.println("Must be a number");
System.exit(1);
}
int [] arr  = {num1, num2, num3, num4, num5};
/* I have a question here. I would like to simply use assert to
* make sure the numbers inputed are positive and not greater than 9
* However, my statement seems to have no effect as I can input
* numbers greater than 9 or zero, with no error messages, why?
*/
for(int i = 0; i<5; i++) assert (arr[i] >0 && arr[i] <9);
int [] arrcopy = new int ;
/*I am copying the array since my original will be modified (see max code below)
* If there was a solution to this too, would be appreciated.
*/
System.arraycopy(arr, 0, arrcopy, 0, 5);
int k = 1;
int max = 0;
/*Here I am calculating the maximum of the numbers so that I know
* the maximum number of rows from the input
*/
for(int i = 0; i <5; i++) {
if(k<5)
if(arr[i]>arr[k]) {
arr[k] = arr[i];
}
k++;
max = arr[k-2];
}
System.out.println (max);
//This is now implementing your suggestion in a code
for( int i = 1; i <max+1; i++) {
for(int j = 0; j<5; j++){
int x = (max+1) - i;
if(x <= arrcopy[j]) System.out.print(str + " ");
else System.out.print(" " + " ");
}
System.out.println();
if( i == max) for(int m = 0; m <5; m++)
System.out.print(arrcopy[m] + " ");
}
}

}```  Reply With Quote

4. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## Re: Display number of characters like bar chart

You an have multiple catch blocks - put the most specific ones first:

Java Code:
```try {
// whatever
} catch(NumberFormatException nfe) {
// deal with it
} catch(Exception e) {
// e will be any *other* sort of exception
}```
Java 7 is a bit different.

-----

By default the compiler doesn't produce any code for asserts. See Compiling Files That Use Assertions in the 1.4 guide.

-----

I wasn't thinking clearly and my reply only made sense because the 5x5 nature of the problem! You don't need to shuffle the array around to find the maximum element. Think about how you figure out the the maximum value on a conveyor belt where one element at a time came into view. You might need a piece of paper (variable) but it need only hold a single value.  Reply With Quote

5. Member Join Date
Oct 2011
Posts
49
Rep Power
0

## Re: Display number of characters like bar chart

Thank you. I have also managed to cut down on the code that calculate the maximum rows using your suggestion. Thanks for the suggestions, it is all appreciated.  Reply With Quote

6. ## Re: Display number of characters like bar chart Originally Posted by pbrockway2 Java 7 is a bit different.
Thanks!! That alone may make it time to upgrade!  Reply With Quote

7. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## Re: Display number of characters like bar chart

You're (both) welcome.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•