Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: Display number of characters like bar chart

  1. #1
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default 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.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.

  3. #3
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default 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] + " ");		     		
    	}	
    	}
    
    }

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.
    Fubarable likes this.

  5. #5
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default 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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Display number of characters like bar chart

    Quote Originally Posted by pbrockway2 View Post
    Java 7 is a bit different.
    Thanks!! That alone may make it time to upgrade!

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Display number of characters like bar chart

    You're (both) welcome.

Similar Threads

  1. display utf8 characters with itext
    By dany_future in forum AWT / Swing
    Replies: 0
    Last Post: 04-22-2011, 03:01 PM
  2. Replies: 0
    Last Post: 02-22-2011, 08:27 AM
  3. How to Display Hindi Characters?
    By sayan751 in forum AWT / Swing
    Replies: 1
    Last Post: 03-03-2009, 03:23 PM
  4. Replies: 8
    Last Post: 12-29-2008, 07:12 AM
  5. Demo bar chart and pie chart
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 11:22 PM

Posting Permissions

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