I have class date which just simply creates an arrayList of days of the week.
import java.util.*;
public class Date {
public static final String[] daysofTheWeek = {"Mon" , "Tue" , "Wed" , "Thurs" , "Fri" , "Sat" , "Sun"};
private String dayofweek;
private int day;
private int month;
public int getDate() {
return this.day;
}
public int getMonth() {
return this.month;
}
public Date(String dayofweek, int day, int month) {
this.day = 1;
if (day >= 1 && day <= 31) {
this.day = day;
}
this.month = 12;
if (month >= 1 && month <= 12) {
this.month = month;
}
this.dayofweek = "Mon";
if (isValidDOW(dayofweek)) { // why is this in the parameters?
this.dayofweek = dayofweek;
}
}
// must validate is DOW
// TRUE/FALSE is the day of the week matching?
public boolean isValidDOW(String d) {
for (int i = 0; i < dayofweek.length(); i++)
if (d.equals(daysofTheWeek[i])) { // why i?
return true;
}
return false;
}
// to string
public static String monthToString(int i) {
switch(i) {
case 1: return "January";
case 2: return "Feb";
case 3: return "March";
case 4: return "April";
case 5: return "May";
case 6: return "June";
case 7: return "July";
case 8: return "August";
case 9: return "September";
case 10: return "October";
case 11: return "November";
case 12: return "December";
default: return "Invalid Month!!";
}
}
public String toString() {
return this.dayofweek + "," + Date.monthToString(this.month) + " " + this.day;
}
public String getDayOfWeek() {
return this.dayofweek;
}
}
Now I am trying to get all days ( Say, return number of times sundays pop up). I tried using a loop to go through and count everything, but I am getting a null pointer exception. I've been looking at this for a while, and can't figure out whats wrong with it. Can anyone guide me in the right direction?
import java.util.*;
/**
* ArrayList example
*/
public class Calendar {
private ArrayList<Date> Schedule;
public ArrayList<Date> sundays(String dayofweek) {
ArrayList<Date> app = new ArrayList<Date>();
for (Date entry : Schedule) {
String d = entry.getDayOfWeek();
if (entry.getDayOfWeek() == dayofweek) {
app.add(entry);
}
else if (app.size() == 0) {
return null;
}
}
return app;
}
}