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 13 of 13

Thread: Display patterns on the screen

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Display patterns on the screen

    I am a beginner in java programming and I need help to use nested loop to print out the following pattern on the screen. The first number in each pair represents the row number and the second one represents the column number.

    The sample output is:

    1,1 1,2 1,3 1,4

    2,1 2,2 2,3 2,4

    3,1 3,2 3,3 3,4

    Here is what I have done so far:

    /*Use nested loop to print out the following pattern on the screen*/

    class Pattern {

    public static void main(String[] args){


    int row =3;
    int col =4;

    for(i =0; i<row; i++)
    {
    for(intj=0; j<col; j++)
    {

    System.out.println(1,1 );




    }

    }

    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Display patterns on the screen

    Have you determined the rules for generating the contents of each line?
    Can you explain what they are?
    For example columun one appears to be the line number, starting at 1.
    What goes in the rest of the columns on the line?

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Display patterns on the screen

    Thanks for the response.
    Here are the responses to your questions:

    The row numbers stay constant but the column number increases
    2, 3, 4 go in the rest of the columns on the line.

    Thanks.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Display patterns on the screen

    If you have the rules for generating the numbers that go on each line, try writing the program that will produce those numbers.

    I don't see that your explanation shows how to generate numbers such that the second column is all 1s and the fourth column is all 2s.

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Display patterns on the screen

    Here is the assignment question:

    Use nested loop to print out the following pattern on the screen: the first number in each pair represents the row number and the second one represents the column number. You need to declare and use tow "int" variables: the total number of rows and the total number of columns. So the actual input will change accordingly if the numebr of rows/columns is changed. For example, the following example is printed when the number of rows is set to 3 and the number of columns is set to 4.

    Output: 1,1 1,2 1,3 1,4
    2,1 2,2 2,3 2,4
    3,1 3,2 3,3 3,4


    The Other Questions is:
    Based on question above, this time only display the number pair when the row number and the column number are the same, otherwise display * . Use both "for" and "while" loop to achieve the same results.

    Thanks for your help!

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Display patterns on the screen

    Have you worked out what the contents of each row is supposed to be and how to generate them?
    This sentence tells you:
    the first number in each pair represents the row number
    and the second one represents the column number.
    You will need to use some loops to print out the numbers. You need to consider when to output the ',' and when the ' '.

  7. #7
    Junior Member
    Join Date
    Feb 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Display patterns on the screen

    Could you please help me with the solution since I need to submit it tonight. Thanks in advance.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Display patterns on the screen

    Start with printing out one row of output row,col number pairs built from these: row number, a comma, column number and a space
    Use a loop to change the column number value from 1 to 4.
    print it on one line by using the print() method for each r,c pair.
    After all the data for one line has been printed in the loop, use the println() method to move to the next line.

  9. #9
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    The code you posted in your first post is quietly correct.
    Instead of writing
    System.out.println(1,1);
    , try to substitute the static numbers with the counters of the loop i and j.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Display patterns on the screen

    Does that println statement compile?

  11. #11
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    If the statement (I cut some text) println(1,1) doesn't compile it's because println needs a string to be passed to and you pass two numbers (you should use "1,1"). Indeed it's correct, it's not what you need: it will print "1,1" all times!
    You should use format("%d,%d%n", i, j) instead (again, I cut some text).
    Last edited by felimblack; February 10th, 2012 at 12:38 PM.

  12. #12
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Display patterns on the screen

    Quote Originally Posted by felimblack View Post
    You should to use println(i, j) instead (again, I cut some text).

    Where in the API for PrintStream is the method definition for println(int, int) ?
    PrintStream (Java Platform SE 6)
    Hint: it doesn't exist.

  13. #13
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by copeg View Post
    Where in the API for PrintStream is the method definition for println(int, int) ?
    PrintStream (Java Platform SE 6)
    Hint: it doesn't exist.
    I apologize, I sent a post I hadn't revised (there were also grammar errors...).
    @copeg, thank you for your extreme efficiency as moderator!

Similar Threads

  1. [SOLVED] Display longest string on screen
    By mwardjava92 in forum Object Oriented Programming
    Replies: 1
    Last Post: January 29th, 2012, 12:12 PM
  2. Replies: 9
    Last Post: December 31st, 2011, 01:22 AM
  3. Changing from a screen to another!
    By gargamel7 in forum Java Theory & Questions
    Replies: 23
    Last Post: October 10th, 2011, 06:27 AM
  4. Loop Patterns (triangles) [Beginner]
    By me1010109 in forum Loops & Control Statements
    Replies: 2
    Last Post: October 24th, 2010, 05:13 PM
  5. clear screen.
    By chronoz13 in forum Java Theory & Questions
    Replies: 10
    Last Post: December 5th, 2009, 07:27 AM