Reputation: 1
After compiling my java program I get a bunch of errors and I don't know how to resolve them. Can anyone give me a hand? =/
class Date{
public static boolean isLeapYear(int year){
boolean answer = false;
if(year % 4 == 0 &&(year % 400 == 0 || year % 100 != 0))
answer = true;
else
answer = false;
return answer;
}
}
int returnDaysInMonth (int year, int month){
if (month==1||month==3||month==5||month==7||month==8||month==10||month==12){
return 31;
} else if (month==4||month==6||month==9||month==11){
return 30;
} else if(isLeapYear(year)){
return 29;
} else {
return 28;
}
}
Here are the compiling errors:
/Users/vlop/NetBeansProjects2/JavaLibrary1/src/Date.java:22: class, interface, or enum expected
public int returnDaysInMonth (int year, int month){
/Users/vlop/NetBeansProjects2/JavaLibrary1/src/Date.java:28: class,
interface, or enum expected
} else if (month==4||month==6||month==9||month==11){
/Users/vlop/NetBeansProjects2/JavaLibrary1/src/Date.java:32: class,
interface, or enum expected
} else if(isLeapYear(year)){
/Users/vlop/NetBeansProjects2/JavaLibrary1/src/Date.java:35: class,
interface, or enum expected
} else {
/Users/vlop/NetBeansProjects2/JavaLibrary1/src/Date.java:38: class,
interface, or enum expected
}
5 errors
Upvotes: 0
Views: 139
Reputation: 328
it should look like this
class Date {
public static boolean isLeapYear(int year) {
boolean answer = false;
if (year % 4 == 0 && (year % 400 == 0 || year % 100 != 0))
answer = true;
else
answer = false;
return answer;
}
int returnDaysInMonth(int year, int month) {
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
|| month == 10 || month == 12) {
return 31;
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
return 30;
} else if (isLeapYear(year)) {
return 29;
} else {
return 28;
}
}
}
Upvotes: 0
Reputation: 6279
These are simplified method to find leap year.
public static boolean isLeapYear(int year) {
assert year >= 1583; // not valid before this date.
return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}
Upvotes: 0
Reputation: 160170
You're trying to do a bunch of Java after your class declaration has been closed.
Proper indentation helps identify such issues:
class Date {
public static boolean isLeapYear(int year) {
boolean answer = false;
if(year % 4 == 0 &&(year % 400 == 0 || year % 100 != 0))
answer = true;
else
answer = false;
return answer;
}
} // End of class.
// WAT
int returnDaysInMonth (int year, int month) {
// etc.
Also, separate issue, but you could return the result of the boolean expression directly:
public static boolean isLeapYear(int year) {
return ((year % 4 == 0) && ((year % 400 == 0) || (year % 100 != 0)));
}
Even if you're not comfortable with that, you could still avoid some noise:
public static boolean isLeapYear(int year) {
if (year % 4 == 0 &&(year % 400 == 0 || year % 100 != 0)) {
return true;
}
return false;
}
Upvotes: 1
Reputation: 328
your brackets are all messed up resulting in the class not being able to be resolved, are you using a compiler or ide like eclipse to program this because it should be telling you that there is an issue
Upvotes: 0