Display number of characters like bar chart

• 10-15-2011, 08:45 PM
Dreaming
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:

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.
• 10-15-2011, 09:53 PM
pbrockway2
Re: Display number of characters like bar chart
Quote:

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:

Code:

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

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.
• 10-16-2011, 04:13 AM
Dreaming
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:

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[0];                 int num1 = 0, num2 = 0, num3 = 0, num4 = 0, num5 = 0;                                try {                                        num1 = Integer.parseInt(args[1]);                         num2 = Integer.parseInt(args[2]);                         num3 = Integer.parseInt(args[3]);                         num4 = Integer.parseInt(args[4]);                         num5 = Integer.parseInt(args[5]);                                        }                 /*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 [5];             /*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] + " ");                                            }                } }```
• 10-16-2011, 06:44 AM
pbrockway2
Re: Display number of characters like bar chart
You an have multiple catch blocks - put the most specific ones first:

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.
• 10-17-2011, 03:00 AM
Dreaming
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.
• 10-17-2011, 03:30 AM
Fubarable
Re: Display number of characters like bar chart
Quote:

Originally Posted by pbrockway2
Java 7 is a bit different.

Thanks!! That alone may make it time to upgrade!
• 10-17-2011, 06:11 AM
pbrockway2
Re: Display number of characters like bar chart
You're (both) welcome.