Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Patterns!! Please help!

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Patterns!! Please help!

    I've been trying to write a program for the last 3 hours with no success. What probably should seem easy to most I'm having difficulty with. I have to print two separate patterns on the same program.

    I've selected the following two:

    1
    1 2
    1 2 3
    1 2 3 4
    1 2 3 4 5
    1 2 3 4 5 6


    1 2 3 4 5 6
    X 1 2 3 4 5
    X X 1 2 3 4
    X X X 1 2 3
    X X X X 1 2
    X X X X X 1

    Imagine the X's are not there on the one above. Every time I previewed the post it showed it a different way without the X's.

    This is the two patterns stacked up, any ideas?

    Here's what I have so far and it gets me to the first line of pattern 2:

    public class TwoPatterns {
    public static void main(String[] args) {

    for (int a = 1; a <= 6; a++) {
    for (int b = 1; b <= a; b++) {
    System.out.printf("%-4d", b);
    }
    System.out.println();
    }
    for (int c = 6; c >= 6; c--) {
    for (int d = 1; d <= c; d++) {
    System.out.printf("%-4d", d);
    }
    System.out.println();
    }
    }
    }

    any help would be so appreciated!


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,512
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: Patterns!! Please help!

    On each line you need to print M number of spaces before you start printing the numbers. Surely you can see a pattern in how many spaces are needed.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Patterns!! Please help!

    Quote Originally Posted by Junky View Post
    On each line you need to print M number of spaces before you start printing the numbers. Surely you can see a pattern in how many spaces are needed.

    I understand that I need to probably have an if/else statement in there that displays a space (" ") if the column is less than or equal to the row but I'm not sure where to put it. I played around with a loop after the second set of nested fors but I could never get it to print what I needed, it seemed to always print on the same line as the first set.

    I then tried to enter in more println but that obviously didn't do the trick, it just spaced out my program more. Do I not need the 3rd and 4th for loops? Should I be doing the if loop directly after the 2nd for loop?

    --- Update ---

    This is the other version I was playing with:

    public class TestPatterns {
    public static void main(String[] args) {

    //Set how many rows you want per pattern
    int column = 12;

    for (int a = 1; a <= column; a++) {

    for (int b = 1; b <= column; b++) {
    if(b < a)
    System.out.print(" ");
    else
    System.out.printf("%-4d", b);
    }
    System.out.println();
    }
    }
    }

  4. #4
    Junior Member
    Join Date
    Nov 2013
    Posts
    16
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Patterns!! Please help!

    try this

    for(int i=1;i<7;i++){
     
       for(int k=1;k<=i;k++)
         System.out.print(k);
     
          System.out.println()  ///// for LINE Breaking Work With Outer Loop
         } 
     
     
    second petren 
     
    for(int i=1;i<7;i++){
     
       for(int k=1;k<i;k++)
         System.out.print(" ");
       for(int m=6;m<=i; m++)
          System.out.print(m)
     
          System.out.println()  ///// for LINE Breaking Work With Outer Loop
         }

  5. The Following User Says Thank You to syedfahadjalali For This Useful Post:

    winn1me (November 14th, 2013)

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,154
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Patterns!! Please help!

    @syedfahadjalali Please don't spoonfeed code
    If you don't understand my answer, don't ignore it, ask a question.

  7. #6
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Patterns!! Please help!

    Although that's helpful, Norm is right. I'm looking for the logic so I can understand how to code this in the future. I am, however; grateful for your post, unlike some that assume everyone knows.

    Mike

  8. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,267
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Patterns!! Please help!

    Suggested reading to @winn1me and @syedfahadjalali
    The Problem with Spoon-feeding

  9. #8
    Junior Member
    Join Date
    Nov 2013
    Posts
    16
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Patterns!! Please help!

    Extremely Sorry. i will be carefull

  10. #9
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,512
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: Patterns!! Please help!

    This type of exercises always pop up. One way to handle them is to write a method that takes two parameters: a char (the character to be printed) and an int (the number of times to print that character). In your main method you simply call your helper method passing in a space and the number of times it has to be printed. If you use some sort of counter then you need to increment or decrement it. Then you can print out the digits.
    Improving the world one idiot at a time!

Similar Threads

  1. Design Patterns
    By keepStriving in forum Java Theory & Questions
    Replies: 3
    Last Post: October 29th, 2013, 11:22 AM
  2. Design Patterns / Architectures
    By davaholic in forum Java Theory & Questions
    Replies: 2
    Last Post: December 18th, 2012, 05:24 PM
  3. Display patterns on the screen
    By finjws in forum Loops & Control Statements
    Replies: 12
    Last Post: February 10th, 2012, 12:11 PM
  4. Loop Patterns (triangles) [Beginner]
    By me1010109 in forum Loops & Control Statements
    Replies: 2
    Last Post: October 24th, 2010, 05:13 PM

Tags for this Thread