that is expected output is

sun   mon   tue  wed  thu  fri  sat
                       1    2    3
4      5     6    7     8   9    10
....
...
...
29  30  31

my code:

static void print_31days(int a[][]){
	
	int i=1;
	while(i<=31)
 {
		int some=0;//some is the startdate of the month of the 
		while( some<5){
			some++;
			System.out.print("---"+"\t");
		}
		
		for(int k=0;k<5&&i<=31;k++){//k is the number of weeks
			
		for(int j =1;j%8!=0&&i<=31;j++){//j is the number of days
			
			a[k][j]=i;
			i++;
			//System.out.println("\t"+"\t");
			
			
			
				if(j==3){
				System.out.println("");
			}
			System.out.print(a[k][j]);
			System.out.print("\t");
			
		}
		System.out.println("");
		}
	}
}

output wat i got is this

enter month
jan\
enter the number of the month
1
month is jan\
number of month is 1

sun	mon	tue	wed	thur	fri	sat	
================================================================================================
sat 	sun 	mon 	tue 	wed 	thu 	fri 	
---	---	---	---	---	1	2	
3	4	5	6	7	
8	9	
10	11	12	13	14	
15	16	
17	18	19	20	21	
22	23	
24	25	26	27	28	
29	30	
31

Dani AI

Generated

The output problems come from three related issues: the start-offset is being recomputed each outer iteration, inner-loop bounds and newline logic produce extra line breaks, and using tabs + 1-based indices makes alignment and indexing error-prone. A robust, simple pattern is: compute the weekday of the 1st once, then walk a fixed 6×7 grid (weeks × days), printing blanks until the first-day, printing day numbers while they remain, and stopping when the month is exhausted.

A compact, easy-to-follow implementation (original example) follows that pattern:

static void printMonth(int firstDay, int daysInMonth) {
    int day = 1;
    for (int week = 0; week < 6; week++) {
        for (int dow = 0; dow < 7; dow++) {
            if (week == 0 && dow < firstDay) {
                System.out.print("    ");
            } else if (day <= daysInMonth) {
                System.out.printf("%3d ", day++);
            } else {
                System.out.print("    ");
            }
        }
        System.out.println();
        if (day > daysInMonth) break;
    }
}

Key points and troubleshooting tips: use zero-based indices (dow 0..6), compute firstDay once (don’t reset it inside the printing loop), stop the outer loop when the counter passes the last day, and prefer fixed-width formatting (printf("%3d ")) instead of tabs so single- and double-digit days line up. If storing the layout, a 6×7 int grid is natural; a 1D approach with counters is simpler if only printing is required.

This agrees with ’s suggestion to use fixed inner bounds and remove the ad‑hoc newline logic, and with ’s point that array dimensions matter (a full 6×7 grid is safe). For production code, ’s note to use the standard date APIs is recommended: LocalDate.of(year, month, 1).getDayOfWeek() gives the correct first-day so the calendar logic doesn’t have to guess.

Recommended Answers

All 6 Replies

try k less than 7...it most likely won't work perfectly, but it will give you a hint. tell us what does your int[][] a parameter to the method hold. it's dimensions. besides, you could reach the effect with a one-dimensional array as well...

nope its nt workin with k<7.

for(int j =1;j%8!=0 ...
why not just
for(int j =1;j<8 ...

if(j==3){System.out.println("");}
looks very wrong, and is why you are getting extra line breaks. Remove it and the formatting is OK apart from the very first line. I suspect the mystery thing you are doing with "some" at the start is to do with getting the first line offset right, so you probably need to use this to get the first line printed OK

well, remove lines 22 to 24 first. you do not need that if just yet. then see what .you get, perhaps that will direct you.

and why don't use java.util.Calendar or Date (and only hard code Month + Year for expected and correct output)

no i wanted to implement on my own ,was stuck in the middle so thought of getting help from techies.
thanks guys i got it.

Be a part of the DaniWeb community

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