The following BlueJ program is a guess question for ISC students. Similar posts for ISC students will be done time to time.
Design a program which inputs a date in six digit number format i.e. 141296 . Test the validity of the date and print the date in full form . If date is invalid then print a message as “ Invalid Date“
1. Example :
INPUT : 141296
OUTPUT : 14th December , 96
: VALID DATE
2. Example :
INPUT : 230488
OUTPUT : 23rd April , 88
: VALID DATE
3. Example :
INPUT : 300284
OUTPUT : INVALID DATE
import java.io.*;
class NumToWord
{
public void show() throws IOException
{
int flag,leap,i,num,mon1,day1,year1;
int dpm[]={31,28,31,30,31,30,31,31,30,31,30,31};
leap=0;
flag=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
clrscr();
System.out.println("\nInput:");
num=Integer.parseInt(br.readLine());
year1=num%100;
num=num/100;
mon1=num%100;
num=num/100;
day1=num;
if(year1%100==0 && year1%400==0)
leap=1;
else if(year1%100!=0 && year1%4==0)
leap=1;
if(mon1==2 && leap==1 && dpm[mon1-1]>28)
flag=1;
else if(mon1==2 && leap==0 && dpm[mon1-1]>28)
flag=1;
else if(day1>dpm[mon1-1])
flag=1;
if(flag==1)
{
System.out.println("\nInvalid Date...");
return;
}
leap=day1%10;
System.out.println("\nOUTPUT: "+ day1);
switch(leap)
{
case 0:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
System.out.print(" th");
break;
case 1:
System.out.print(" st");
break;
case 2:
System.out.print(" nd");
break;
case 3:
System.out.print(" rd");
break;
}
switch(mon1)
{
case 1:
System.out.print(" January,");
break;
case 2:
System.out.print(" February,");
break;
case 3:
System.out.print(" March,");
break;
case 4:
System.out.print(" April,");
break;
case 5:
System.out.print(" May,");
break;
case 6:
System.out.print(" June,");
break;
case 7:
System.out.print(" July,");
break;
case 8:
System.out.print(" August,");
break;
case 9:
System.out.print(" September,");
break;
case 10:
System.out.print(" October,");
break;
case 11:
System.out.print(" November,");
break;
case 12:
System.out.print(" December,");
break;
}
System.out.print(year1);
}
}
After the validation job, it is displayed in proper format using switch statement. Another switch statement is used in this program to display the month name. Year number is displayed at the end.
this method cud also be followed:
ReplyDeleteimport java.io.*;
class valid_date
{
public static void main(String arg[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
int x1,y1,z1,l;x1=y1=z1=l=0;
char b0,b1,b2,b3,b4,b5;
String s,x,y,z,z2,g;x=y=z=g=z2="";
String m[]={"January","February","March","April","May","June","July","August","September","October","November","December"};
System.out.println("Enter a 6 digitted number to be checked for valid date in ddmmyy format");
s=in.readLine();
l=s.length();
if(l==6)//to check if the no of digits are 6 or not
{
x=s.substring(0,2);
y=s.substring(2,4);
z=s.substring(4,6);
b0=s.charAt(0);
b1=s.charAt(1);
b2=s.charAt(2);
b3=s.charAt(3);
b4=s.charAt(4);
b5=s.charAt(5);
if((Character.isDigit(b0))&&(Character.isDigit(b1))&&(Character.isDigit(b2))&&(Character.isDigit(b3))&&(Character.isDigit(b4))&&(Character.isDigit(b5)))
{
x1=Integer.parseInt(x);
y1=Integer.parseInt(y);
z1=Integer.parseInt(z);
if((z1>=0)&&(z1<=15))
z2="20"+z;
else if(z1>15)
z2="19"+z;
if((x1==1)||(x1==21)||(x1==31))
g="st";
else if((x1==2)||(x1==22))
g="nd";
else if((x1==3)||(x1==23))
g="rd";
else
g="th";
if((y1>12)||(y1==0)||(x1==0))
System.out.println("Invalid date");
else if(((y1==1)||(y1==3)||(y1==5)||(y1==7)||(y1==8)||(y1==10)||(y1==12))&&(x1>31))
System.out.println("Invalid date");
else if(((y1==4)||(y1==6)||(y1==9)||(y1==11))&&(x1>30))
System.out.println("Invalid date");
else if(((y1==2)&&(z1%4==0)&&(x1>29))||((y1==2)&&(z1%4!=0)&&(x1>28)))
System.out.println("Invalid date");
else
System.out.print(x1+g+" "+m[(y1-1)]+", "+z2);
}
else
System.out.println("You hav not entered digits...invalid input!!! Try again");
}
else
System.out.println("You hav not entered 6 digits...invalid input!!! Try again");
}
}
Hello Sir,
ReplyDeleteim a 10th std student icse
i would like u to solve a problem for me sir
* Write A Program to input date from the user and print wheter it is a valid date or not
I hope you will help me .
Here is my email id u can mail me the program
tamimsangrar@gmail.com
Thanking You,
Tamim Sangrar.
The program posted on 27 February 2011 (just above your comment) is the program which you need.
ReplyDelete