# Display patterns on the screen

• February 4th, 2012, 09:34 PM
finjws
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 );

}

}

}
• February 5th, 2012, 07:18 AM
Norm
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?
• February 5th, 2012, 06:34 PM
finjws
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.
• February 5th, 2012, 06:38 PM
Norm
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.
• February 5th, 2012, 07:35 PM
finjws
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.

• February 5th, 2012, 07:59 PM
Norm
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:
Quote:

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 ' '.
• February 5th, 2012, 08:59 PM
finjws
Re: Display patterns on the screen
• February 6th, 2012, 05:55 AM
Norm
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.
• February 10th, 2012, 10:57 AM
felimblack
The code you posted in your first post is quietly correct.
Code :

`System.out.println(1,1);`
, try to substitute the static numbers with the counters of the loop i and j.
• February 10th, 2012, 11:06 AM
Norm
Re: Display patterns on the screen
Does that println statement compile?
• February 10th, 2012, 11:20 AM
felimblack
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).
• February 10th, 2012, 11:25 AM
copeg
Re: Display patterns on the screen
Quote:

Originally Posted by felimblack
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.
• February 10th, 2012, 12:11 PM
felimblack
Quote:

Originally Posted by copeg
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!