i think something wrong with array...plz help!!
hey...i wrote this code to check the validity of a date
the errors are at line 8 and mentioned as comment with it!!
Code Java:
import java.io.*;
class Date//checks validity of a date
{
int days []=new int [12];
int d,m,y;
Date ( )
{
days []={31,28,31,30,31,30,31,31,30,31,30,31};// <-- 'not a staement' and '; expected' error
}
void accept( )throws Exception
{
BufferedReader obl=new BufferedReader (new InputStreamReader(System.in));
d=Integer.parseInt(obl.readLine());
m=Integer.parseInt(obl.readLine());
y=Integer.parseInt(obl.readLine());
}
boolean isLeap()
{
if (y%4==0)
return true;
else
return false;
}
boolean checkDate( )
{
if (isLeap == true)
days[1]++;
if(m<=12)
{
if(d<=days[m-1])
{
return true;
}
}
else
return false;
}
public static void main(String args [])throws Exception
{
Date d1=new Date();
d1.accept();
d1.isLeap();
if(d1.checkDate()==true)
System.out.println("Date is valid");
}
}
Re: i think something wrong with array...plz help!!
Code java:
import java.io.*;
public class Date//checks validity of a date
{
int d, m, y;
public Date() {
int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};// Short syntax array - size == amount between { and }
}
/*Or you can use the following syntax:
* int[] days = new int[sizeHere];
* days[0] = 1;
* days[1] = 2;
* days[2] = 3;
* days[3] = 4;
* days[4] = 5;
* days[5] = 6;
*/
public void accept() throws Exception {
BufferedReader obl = new BufferedReader(new InputStreamReader(System.in));
d = Integer.parseInt(obl.readLine());
m = Integer.parseInt(obl.readLine());
y = Integer.parseInt(obl.readLine());
}
public boolean isLeap() {
if (y % 4 == 0) {
return true;
} else {
return false;
}
}
public boolean checkDate() {
if (isLeap == true) {
days[1]++;
}
if (m <= 12) {
if (d <= days[m - 1]) {
return true;
}
} else {
return false;
}
}
public static void main(String args[]) throws Exception {
Date d1 = new Date();
d1.accept();
d1.isLeap();
if (d1.checkDate() == true) {
System.out.println("Date is valid");
}
}
}
There you go, anything you don't understand, just ask.
Re: i think something wrong with array...plz help!!
Quote:
Originally Posted by
newbie
There you go, anything you don't understand, just ask.
I have some things I don't understand
1. Why did you post incorrect code?
2. Why did you define a variable in the constructor, that is never read locally, which generates a warning?
3. Why did you refer to days in checkDate() when it is only defined in the scope of the Date constructor?
4. Why did you leave the () off of isLeap in the checkDate() method. Don't you want to call the isLeap method? If not, why didn't you define a boolean field isLeap as a member of the Date class?
Re: i think something wrong with array...plz help!!
Code java:
public boolean isLeap(int year) {
if (year % 4 == 0) {
return true;
} else {
return false;
}
}
Not sure why you're doing a BufferReader which used an InputStreamReader which uses System.in;
Why not just use a Scanner
Scanner console = new Scanner(System.in);
Also, change your checkDate method to checkDate(int day, month, int year)
A Date has a day, a month, and a year.
public Date(int day, int month, int year)
If year is leap year, accept February dates from 1 to 29.
Otherwise accept February dates only from 1 to 28.
Accept dates as valid for the other months as long as they are within the appropriate ranges.
Don't accept months less than 1 or greater than 12.
Don't know how you want to do the year thing.
Re: i think something wrong with array...plz help!!
thnx...it was giving errors but i worked it out later!!
Re: i think something wrong with array...plz help!!
n thnx javapenguin n david fongs....u cleared my concepts!!
Re: i think something wrong with array...plz help!!
Quote:
Originally Posted by
DavidFongs
I have some things I don't understand
1. Why did you post incorrect code?
2. Why did you define a variable in the constructor, that is never read locally, which generates a warning?
3. Why did you refer to days in checkDate() when it is only defined in the scope of the Date constructor?
4. Why did you leave the () off of isLeap in the checkDate() method. Don't you want to call the isLeap method? If not, why didn't you define a boolean field isLeap as a member of the Date class?
Possibly because he asked for correction of his Array? Which is what I believe I did.
Sorry If I didn't read his entire program... but I let him keep his original structure because I don't know what his specifications are.