code above would print first month correct. but from the second month days or not in order.

import java.io.*;
public class calendertest {

	
	public static void main(String[] args) {
	int months=12;
	int days=0;
	int i=0;
	for(months=1;months<=12;months++)
        {
             System.out.println("----------------------");
             if(months%2==1)
		{
			for(days=0;days<=31;days++)
 			{   
			    if (days%7==1)
 			    System.out.println("sunday-"+days);
			    else if(days%7==2)
			    	System.out.println("monday-"+days);
			    else if(days%7==3)
			    	System.out.println("tuesday-"+days);
			    else if(days%7==4)
			    	System.out.println("wedday-"+days);
			    else if(days%7==5)
			    	System.out.println("thursday-"+days);
			    else if(days%7==6)
			    	System.out.println("friday-"+days);
			    else if(days%7==0)
			    	System.out.println("saturday-"+days);
			
 			}
			System.out.println('\n');
			
		}
             System.out.println("----------------------");
	     if(months%2==0)
		{	
			for(days=0;days<=30;days++)
 			{
				if (days%7==1)
	 			    System.out.println("sunday-"+days);
				    else if(days%7==2)
				    	System.out.println("monday-"+days);
				    else if(days%7==3)
				    	System.out.println("tuesday-"+days);
				    else if(days%7==4)
				    	System.out.println("wedday-"+days);
				    else if(days%7==5)
				    	System.out.println("thursday-"+days);
				    else if(days%7==6)
				    	System.out.println("friday-"+days);
				    else if(days%7==0)
				    	System.out.println("saturday-"+days);
							
 			}
		}
	}
}
}

Recommended Answers

All 12 Replies

You are starting your days from 0 and looping to 31 that makes a total of 32 days every month

ok i initialised days to 1.

one thing more you are not holding the last day of previous month anywhere because you know not every month starts from Monday which is happening there in your application and also the program should consider 365 days not 30 or 31 days of each month i hope you under stand

so should i use any other data structures ? if yes which one ?
and one more thing in java everytime the controls enter into for loop the output is printed on the next line.

everytime the controls enter into for loop the output is printed on the next line.

You print using println(...), which always adds a newLine char at the end of whatever you print. If you use print(...) instead then the next printout will continue on the same line.

made some changes or code with diff logic

import java.util.Scanner;
import java.io.*;
public class sample1 {

	public static void main(String[] args) {
		int [][] a;
		a=new int[10][10];
		System.out.println("enter month and the number of the month");
				 Scanner scanner = new Scanner( System.in );
				String month= scanner.nextLine();
		 System.out.println("enter  the number of the month");
		 String month_no= scanner.nextLine();
		 int mo = Integer.parseInt( month_no );  
		 System.out.println(month);
		 System.out.println(mo);
		 switch(mo){
		 case 1:
		 case 3:
		 case 5:
		 case 7:
		 case 8:
		 case 10:
		 case 12:
			 System.out.println(" month with 31 days");
			 int i=1;
				while(i<=31){
					System.out.println("sun    mon    tue     wed   thur     fri    sat");
					System.out.println("i= "+mo);
					for(int k=0;k<5;k++){
					for(int j =1;j%8!=0;j++){
						
						a[k][j]=i;
						i++;
						System.out.print(a[k][j]);
						System.out.print("\t");
					}
					System.out.println("");
					}
				}
			 break;
		 default:
			 System.out.println("month with 30 days ");
					 
		 }
	}

}

output:

enter month and the number of the month
jan
enter the number of the month
1
jan
1
month with 31 days
sun mon tue wed thur fri sat
i= 1
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 32 33 34 35


question :
number of days is more than 3 days , can anybody tel wats the logical error.

sorry:
number of days is more than 31 days.

You print the days in your inner nested loops ~line 31, counting them in i++ as you go, BUT you don't test i until you exit both those loops and restart the while loop on line 26.

thank you i got that.

I have changed the code now thecode is priting according to the input of the user

import java.util.Scanner;
import java.io.*;
public class sample1 {

	public static void main(String[] args) {
		int [][] a;
		a=new int[10][10];
		int i=0;
		System.out.println("enter month and the number of the month");
				 Scanner scanner = new Scanner( System.in );
				String month= scanner.nextLine();
		 System.out.println("enter  the number of the month");
		 String month_no= scanner.nextLine();
		 int mo = Integer.parseInt( month_no );  
		 System.out.println("month is "+month);
		 System.out.println("number of month is "+mo);
		 switch(mo){
		 case 1:
		 case 3:
		 case 5:
		 case 7:
		 case 8:
		 case 10:
		 case 12:
			 
			 /*printing months which has 31 days*/
			  i=1;
				while(i<=31)
			 {
					System.out.println("sun    mon    tue     wed   thur     fri    sat");
					System.out.println("i= "+mo);
					
					/*You print the days in your inner nested loops ~line 31, 
					 * counting them in i++ as you go,
					 *  BUT you don't test i until you exit
					 *   both those loops and restart the while loop on line 26. */
					
					
					for(int k=0;k<5&&i<=31;k++){
					for(int j =1;j%8!=0&&i<=31;j++){
						
						a[k][j]=i;
						i++;
						System.out.print(a[k][j]);
						System.out.print("\t");
						
					}
					System.out.println("");
					}
				}
			 break;
			 
		 /*checkin leap year and printing feb month*/
		 
		 case 2:
			 System.out.println("you have entered february month enter year to chk wheather its leap year or not");
			 String year= scanner.nextLine();
			 int yr = Integer.parseInt( year );
			 int p=1;
			 if(yr%4==0){
			 
				while(p<=5)
			 {
					System.out.println("sun    mon    tue     wed   thur     fri    sat");
					System.out.println("i= "+mo);
					
					
					/*You print the days in your inner nested loops ~line 31, 
					 * counting them in i++ as you go,
					 *  BUT you don't test i until you exit
					 *   both those loops and restart the while loop on line 26. */
			
					
					for(int k=0;k<5&&p<=29;k++){
					for(int j =1;j%8!=0&&p<=29;j++){
						
						a[k][j]=p;
						p++;
						System.out.print(a[k][j]);
						System.out.print("\t");
						
					}
					System.out.println("");
					}
				}
			 }else {
				 while(p<=5)
				 {
						System.out.println("sun    mon    tue     wed   thur     fri    sat");
						System.out.println("i= "+mo);
						
						
						/*You print the days in your inner nested loops ~line 31, 
						 * counting them in i++ as you go,
						 *  BUT you don't test i until you exit
						 *   both those loops and restart the while loop on line 26. */
				
						
						for(int k=0;k<5&&p<=28;k++){
						for(int j =1;j%8!=0&&p<=28;j++){
							
							a[k][j]=p;
							p++;
							System.out.print(a[k][j]);
							System.out.print("\t");
							
						}
						System.out.println("");
						}
					}
				 
			 }
			 break;
			
			 
			 
			 /*printing months with 30 days*/	 
		  default:
			 System.out.println(month+" month with 30 days ");
		 int q=1;
				while(q<=5)
			 {
					System.out.println("sun    mon    tue     wed   thur     fri    sat");
					System.out.println("i= "+mo);
					
					
					/*You print the days in your inner nested loops ~line 31, 
					 * counting them in i++ as you go,
					 *  BUT you don't test i until you exit
					 *   both those loops and restart the while loop on line 26. */
				for(int k=0;k<5&&q<=30;k++){
					for(int j =1;j%8!=0&&q<=30;j++){
						
						a[k][j]=q;
						q++;
						System.out.print(a[k][j]);
						System.out.print("\t");
						
					}
					System.out.println("");
					}
				}
					 
		 }
	}

}

output:
enter month and the number of the month
jan
enter the number of the month
1
month is jan
number of month is 1
sun mon tue wed thur fri sat
i= 1
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

question :
months in the logic start with sunday (whatever the month is).
so could anybody give some idea how to deal with that(dont mind m still new to java).

at the end comings question

1/ why don't use java.util.Date or Calendar ...

If the month does not start with a Sunday you need to start your printout from the Sunday immediately before the first day of the month. Pseudocode:

Date startDate = first day of month
while (startDate is not a Sunday) startDate = startDate - (one day);
print calendar starting from startDate, print blanks instead of day if date in is previous month]

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.