Page 1 of 2 12 LastLast
Results 1 to 20 of 37
  1. #1
    arunjib is offline Member
    Join Date
    Mar 2011
    Location
    West Bengal
    Posts
    29
    Rep Power
    0

    Default Need help on java

    i want to print the following pattern, please help me

    *******
    ******
    *****
    ****

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

  3. #3
    arunjib is offline Member
    Join Date
    Mar 2011
    Location
    West Bengal
    Posts
    29
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    What do you have so far? What are your current ideas?
    I just tried the program as below
    Java Code:
    import java.io.*;
    public class pattern1
    {
    public static void main (String[] args) throws IOException
    {
    InputStreamReader read = new InputStreamReader(System.in);
    BufferedReader in = new BufferedReader(read);
    int i,j,n=0;
    System.out.print("How many stars you want to print in first row ? ");
    n=Integer.parseInt(in.readLine());
    for (i = 0; i<n; i++)
    {
    for (j=i; j<n;j++)
    System.out.print ("*");
    System.out.println();
    }
    }
    }
    the output is ---
    How many stars you want to print in first row ? 5
    *****
    ****
    ***
    **
    *
    ......... i just cannot put space left side or can say that i cannot centred the outputs
    Last edited by arunjib; 03-06-2011 at 02:40 PM.

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

    Default

    Your code above is unreadable. Please edit your post above so that your code is indented and readable and place the tag [cod&#101;] above your code block and the tag [/cod&#101;] below your code block so that the forum software will maintain the formatting.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Try defining a static helper to print a single line.
    Java Code:
    public String printRow(int n)
    after designing the code think of a way to work it in and perform what you want.
    Last edited by sunde887; 03-06-2011 at 02:35 PM.

  6. #6
    arunjib is offline Member
    Join Date
    Mar 2011
    Location
    West Bengal
    Posts
    29
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Your code above is unreadable. Please edit your post above so that your code is indented and readable and place the tag [cod&#101;] above your code block and the tag [/cod&#101;] below your code block so that the forum software will maintain the formatting.
    is it ok now??

  7. #7
    arunjib is offline Member
    Join Date
    Mar 2011
    Location
    West Bengal
    Posts
    29
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Try defining a static helper to print a single line.
    Java Code:
    public String printRow(int n)
    after designing the code think of a way to work it in and perform what you want.
    sorry i cannot understand what u want to help me

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Do you know what a static method is? Create one to do the printing of a single line.
    Java Code:
    printRow(5) 
    // produces *****
    You can use this method and a loop to solve this problem.

    Also look into string formatting

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

    Default

    Quote Originally Posted by arunjib View Post
    is it ok now??
    You can answer that: Are all your programs written fully left justified -- all lines start in the 0th column?

    Anyway, regarding the logic, you want to System.out.print(" ") in a for loop before your starts, the number of which will depend on some simple math. Try and play with the numbers and you'll figure it out. Half the fun is in the discovery. Luck!

  10. #10
    arunjib is offline Member
    Join Date
    Mar 2011
    Location
    West Bengal
    Posts
    29
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Do you know what a static method is? Create one to do the printing of a single line.
    Java Code:
    printRow(5) 
    // produces *****
    You can use this method and a loop to solve this problem.

    Also look into string formatting
    thank u for guide. but how can i leave space at left of second, 3rd and so on

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I told you what to search for, I'd rather not give too much away, check out the tutorials and API. See if you can find something that does what you want. By the way, this isn't the only way to handle this problem, just one.

  12. #12
    arunjib is offline Member
    Join Date
    Mar 2011
    Location
    West Bengal
    Posts
    29
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    You can answer that: Are all your programs written fully left justified -- all lines start in the 0th column?

    Anyway, regarding the logic, you want to System.out.print(" ") in a for loop before your starts, the number of which will depend on some simple math. Try and play with the numbers and you'll figure it out. Half the fun is in the discovery. Luck!
    i am really a begineer in java programming... can u please help more to solve this problem

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

    Default

    Quote Originally Posted by arunjib View Post
    thank u for guide. but how can i leave space at left of second, 3rd and so on
    Again, it's nothing more than basic math. Play with your numbers on paper and you'll figure it out.

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    In the posted solution, tell me how many leading spaces are on each line? What variable changes, and which doesn't? Any way you can do math to figure something out?

    Take some time and think about how to do this, it is trivial for us, but since it's a challenge for you it is a great learning experience. try to work on small pieces of the puzzle and build up to the solution.
    Last edited by sunde887; 03-06-2011 at 03:18 PM.

  15. #15
    arunjib is offline Member
    Join Date
    Mar 2011
    Location
    West Bengal
    Posts
    29
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    In the posted solution, tell me how many leading spaces are on each line? What variable changes, and which doesn't? Any way you can do math to figure something out?

    Take some time and think about how to do this, it is trivial for us, but since it's a challenge for you it is a great learning experience. try to work on small pieces of the puzzle and build up to the solution.
    let us print
    line 1: 5 [*****]
    line 2: 3 [ *** ] 1 leading space and 1 trailing space
    line 3: 1 [ * ] 2 leading space and 2 trailing space

    so how can i use the loop?

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by arunjib View Post
    sorry i cannot understand what u want to help me
    Think a bit and don't guess; if you have to print n rows, the first row (row #0) contains 2*n-1 stars, and no leading space; the last line (row #n-1) contains 1 star and n-1 spaces. Line k prints 2*(n-k)-1 stars and k spaces. Use the following little method to print your stars:

    Java Code:
    private static void printRow(char c, int n) {
       for (int i= 1; i <= n; i++)
          System.out.print(c);
    }
    kind regards,

    Jos
    Last edited by JosAH; 03-06-2011 at 04:23 PM.
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    arunjib is offline Member
    Join Date
    Mar 2011
    Location
    West Bengal
    Posts
    29
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Think a bit and don't guess; if you have to print n rows, the first row contains 2*n+1 stars, and no leading space; the last line contains 1 star and n-1 spaces. Line k prints 2*(n-k)+1 stars and k spaces. Use the following little method to print your star:

    Java Code:
    private static void printRow(char c, int n) {
       for (int i= 1; i <= n; i++)
          System.out.print(c);
    }
    kind regards,

    Jos
    now i am feeling shy.... i cannot do anything though you are giving hints to me

  18. #18
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Don't feel shy. We want to help you we are just trying to stay somewhat vague so you have to put in more work. It's hard to grasp this stuff when you are new but if you step back and think about it, it helps a lot. This isn't too challenging button are still a beginner so it should present a challenge. My method was to use string format to produce the answer, try googling string.format. Or use jos's more mathy approach. Either works, just sit back and think.

  19. #19
    arunjib is offline Member
    Join Date
    Mar 2011
    Location
    West Bengal
    Posts
    29
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Don't feel shy. We want to help you we are just trying to stay somewhat vague so you have to put in more work. It's hard to grasp this stuff when you are new but if you step back and think about it, it helps a lot. This isn't too challenging button are still a beginner so it should present a challenge. My method was to use string format to produce the answer, try googling string.format. Or use jos's more mathy approach. Either works, just sit back and think.
    i would be greatful if u post my problem solved. i am so beginner that i cannot try too. if i cannot get any help i have to read more and more to solve this problem. but i need this problem solved within tomorrow.

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

    Default

    Quote Originally Posted by arunjib View Post
    i would be greatful if u post my problem solved. i am so beginner that i cannot try too. if i cannot get any help i have to read more and more to solve this problem. but i need this problem solved within tomorrow.
    That is not how it is done here. Please don't ask again for code solutions or I will have no choice but to lock this thread. Again, please try to use the hints given above, any try, and we will help guide you through, but don't ask for code solutions.

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

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