Results 1 to 7 of 7
- 10-15-2011, 08:45 PM #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:
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.Java Code:X X X X X X X X X X X X X X X 2 3 4 1 5
- 10-15-2011, 09:53 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: Display number of characters like bar chart
If I understand you correctly you would fill your five arrays so they look like:I have a problem with the displaying part. I don't know how I can display it in this manner.
Think about how you would determine what character to print at ? in the following:Java Code:XX___ XXX__ XXXX_ X____ XXXXX
Which array would you check? And which position within that array?Java Code:X X ? X X X X X X X X X X X X X 2 3 4 1 5
-----
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 #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[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 #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: Display number of characters like bar chart
You an have multiple catch blocks - put the most specific ones first:
Java 7 is a bit different.Java Code:try { // whatever } catch(NumberFormatException nfe) { // deal with it } catch(Exception e) { // e will be any *other* sort of exception }
-----
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 #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.
-
- 10-17-2011, 06:11 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Similar Threads
-
display utf8 characters with itext
By dany_future in forum AWT / SwingReplies: 0Last Post: 04-22-2011, 03:01 PM -
Convert PCL5 to PDF, Display UNICODE & ASCII Characters Inside PDF
By sherazam in forum Java SoftwareReplies: 0Last Post: 02-22-2011, 08:27 AM -
How to Display Hindi Characters?
By sayan751 in forum AWT / SwingReplies: 1Last Post: 03-03-2009, 03:23 PM -
How to display a list of items and on click display subitems?
By mandyj in forum New To JavaReplies: 8Last Post: 12-29-2008, 07:12 AM -
Demo bar chart and pie chart
By Java Tip in forum java.awtReplies: 0Last Post: 06-23-2008, 11:22 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks