``````GregorianCalendar cal = new GregorianCalendar(year, month, 1);

nod = cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);

som = cal.get(GregorianCalendar.DAY_OF_WEEK);

for (int i=1; i<=nod; i++){// plz explain this line

int row = new Integer((i+som-2)/7); // plz explain this line.

int column = (i+som-2)%7;// also this line.

mtblCalendar.setValueAt(i, row, column);
``````

Where did you get this code? By just randomly copying code you've found on the net, you won't be learning coding yourself.
`for (int i=1; i<=nod; i++){`
This is the opening of an iteration, a for loop.
The following block will execute while i is equal to or lesser than the …

## All 2 Replies

Where did you get this code? By just randomly copying code you've found on the net, you won't be learning coding yourself.
`for (int i=1; i<=nod; i++){`
This is the opening of an iteration, a for loop.
The following block will execute while i is equal to or lesser than the value of nod.

At the beginning of the iterations, the value of i is set to 1. if 1 <= nod, it will run (it, being the block of the for-loop, so, the code until the closing bracket). At the end of the iteration, i++ will be executed. i++ gives the same result as: i = i + 1;
And then the iteration will run again, until the value of i becomes greater than the value of nod.

`int row = new Integer((i+som-2)/7);`
here is some upcasting going on. You create an instance of the wrapper class Integer, and you downcast that to an int. I assume you understand how the value is set?

`int column = (i+som-2)%7;`
The only thing I think could be considered giving the need of an explanation, is the % operator. Actually, % gives you the 'remaining restvalue after division'. A simple instance of this: determinating whether or not a number is even or odd.

``````int x = getValue();
// let's say you want to know whether or not x is even or odd.
int remainder = x%2;
// if x is even, remainder will be 0, otherwise, after x/2, there will be a remaining restvalue of 1
``````

It's creating a grid to display one month's calendar, one week per row x 7 columns.
It loops through all the days, divides by 7 to get the week (row), and modulos by 7 to get the day (col).
The "som" variable contains the day (Mon, Tues etc) corresponding to the 1st of the month, and is used to shift the days so they appear in the right column.

ps GregorianCalendar and its related classes were replaced in Java 8 with a new, greatly superior, set of classes for dates and times, so you wouldn't use this code in a new application. In any case, it's rubbish code - badly written, unclear and sometime stupid (See stultuske's note on casting).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.