Results 1 to 5 of 5
  1. #1
    hubaba is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default [solved]Problem with creating a string bar chart

    hellu :) im doing an intro to java course at uni and got this assignment that im completely stuck with.. was wondering if someone wants to help me out? :)

    im trying to create
    Java Code:
    **
    **    **
    **    ** **
    ** ** ** **
    ** ** ** **
    ------------
    5  2  4  3
    and atm i get it to look like
    Java Code:
    ** 
    ** 
       ** 
       ** 
       ** 
          ** 
          ** 
    2  3  2
    this is how my source code looks like without the user input..

    Java Code:
    		System.out.print(bar1(num1)+"  "+bar2(num2)+"  "+bar3(num3));
    
    	}
    	
    	public static int bar1(int num1)
    	{
    		for (int i = 1; i <= num1; i++) 
    		{
    		System.out.print("** ");
    		System.out.println();
    		}
    	return num1;
    	}
    	
    	public static int bar2(int num2)
    	{
    		for (int i = 1; i <= num2; i++) 
    		{
    		System.out.print("   ");
    		System.out.print("** ");
    		System.out.println();
    		}
    	return num2;
    	}
    	
    	public static int bar3(int num3)
    	{
    		for (int i = 1; i <= num3; i++) 
    		{
    		System.out.print("      ");
    		System.out.print("** ");
    		System.out.println();
    		}
    	return num3;
    	}
    I cant use array, to begin with. I think im missing a for statement that will push the consecutive bars to the right but I everything ive tried just wont work. So i then I thought that maybe if i make every bar a separate method and put them in a print statement it might work... but only came this far.

    what am I missing? am i far off? feels like i just need the last pice of the puzzle... :confused:
    Last edited by hubaba; 04-04-2010 at 08:48 PM. Reason: solved

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    I don't see how you can do this in separate methods. As you can see:
    Quote Originally Posted by hubaba View Post
    Java Code:
    **
    **    **
    **    ** **
    [COLOR="Red"]** ** ** **[/COLOR]
    ** ** ** **
    ------------
    5  2  4  3
    ...your code has to print *s for all of your values on the same line. That means it's going to have to print them all in the same loop. Just remember that you are printing higher values first, and lower values later. That means this is going to be easier if your loop counts down instead of counting up.

    -Gary-

  3. #3
    hubaba is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by gcalvin View Post
    I don't see how you can do this in separate methods. As you can see:

    ...your code has to print *s for all of your values on the same line. That means it's going to have to print them all in the same loop. Just remember that you are printing higher values first, and lower values later. That means this is going to be easier if your loop counts down instead of counting up.

    -Gary-
    so by printing stuff horizontally instead of vertically is the way to go? how can i make that happen? :P guess the nested loops would look something like

    Java Code:
                            for (int i = bar1; i >= 1; i--) 
    			{
    				for (int j = bar2; j>= 1; j--)
    				{
    					for (int k = bar3; k>= 1; k--)
    					{
    						for (int o = bar4; o >= 1; o--)
    						{
    	//					System.out.print("##");
    						}
    	//				System.out.print(" ");
    					}
    	//			System.out.print(" ");
    				}
    			System.out.print("** ");
    			}
    though I got no clue where to put the print statements... right now its quite all over the place tbh... could u elaborate a bit more? atleast now they print on the same rows... but dont know how to put in the empty spaces :confused:

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Forget the sideways printing. If you're not allowed to use arrays, you're certainly not allowed to switch to sideways printing.

    Don't make it too difficult -- keep it simple.
    Java Code:
    for loop starting at high value and counting down to zero
            if num1 is at least at current value
                    print two *s
            else
                    print two spaces
            print space
            if num2 is at least at current value
                    print two *s
            else
                    print two spaces
            print space
            if num3 is at least at current value
                    print two *s
            else
                    print two spaces
            print end of line
    end loop
    That's how it will work. There is no way to avoid the repeated code since you're not allowed to use arrays. You could refactor a few lines of it into a method, and then call the method on num1, num2, and num3 within the loop, but understand that the method is only going to choose to print *s or spaces on the line you're currently on, and it's going to need the current value as well as the num value.

    -Gary-

  5. #5
    hubaba is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Creating PDF file from a String
    By faroafro in forum Advanced Java
    Replies: 6
    Last Post: 11-17-2009, 12:38 AM
  2. Replies: 7
    Last Post: 10-13-2009, 12:16 AM
  3. Creating Array of Characters From a String
    By ngc0202 in forum New To Java
    Replies: 6
    Last Post: 07-30-2009, 08:30 AM
  4. Demo bar chart and pie chart
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-24-2008, 12:22 AM
  5. Creating objects based on a String value
    By lvh in forum New To Java
    Replies: 4
    Last Post: 04-30-2008, 03:00 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
  •