Question
I am making an event calendar and am having issues with a ArrayIndexOutOfBoundsException Error. It usually pops up whenever n or p is selected in
I am making an event calendar and am having issues with a "ArrayIndexOutOfBoundsException" Error. It usually pops up whenever "n" or "p" is selected in the menu a few times.
The goal of this project is to be able to input events into a calendar and then print them out into a txt folder.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Calendar;
import java.util.Scanner;
public class Assignment2 {
private static int SIZE = 15;
private static char HORIZONTAL = '=';
private static char VERTICAL = '|';
private static boolean showLine = true;
private static int dayValue = -1;
private static String[][] eventArray;
public static boolean loadEventsFromFile(String filename) {
eventArray = new String[12][];
for(int i = 0; i < 12; i++)
eventArray[i] = new String[getMaximumDays(i)];
try {
Scanner input = new Scanner(new File(filename));
while(input.hasNext()) {
String date = input.next();
String event = input.nextLine().trim();
int day = dayFromDate(date);
int month = monthFromDate(date);
eventArray[month-1][day-1] = event;
}
input.close();
return true;
}
catch (FileNotFoundException e) {
return false;
}
}
public static void drawMonth(PrintStream out, int month, int daysToSkip, int maxDays) {
for(int i = 0; i < (SIZE * 7) / 2 - 3; i++) {
System.out.print(" ");
}
if (month == 1) {
out.println("January");
}
else if (month == 2) {
out.println("February");
}
else if (month == 3) {
out.println("March");
}
else if (month == 4) {
out.println("April");
}
else if (month == 5) {
out.println("May");
}
else if (month == 6) {
out.println("June");
}
else if (month == 7) {
out.println("July");
}
else if (month == 8) {
out.println("August");
}
else if (month == 9) {
out.println("September");
}
else if (month == 10) {
out.println("October");
}
else if (month == 11) {
out.println("November");
}
else if (month == 12) {
out.println("December");
}
for(int i = 0; i < 7; i++) {
for(int j = 0; j < SIZE / 2 -1 ; j++) {
out.print(" ");
}
if(i == 0) {
out.print("SUN");
} else if (i == 1) {
out.print("MON");
} else if (i == 2) {
out.print("TUE");
} else if (i == 3) {
out.print("WED");
} else if (i == 4) {
out.print("THU");
} else if (i == 5) {
out.print("FRI");
} else {
out.print("SAT");
}
for(int j = 0; j < SIZE / 2 - 2; j++) {
out.print(" ");
}
}
out.println();
int start = 1;
while (start <= maxDays) {
if(start == 1) {
drawRow(out, start, month, maxDays, daysToSkip);
start += 7 - daysToSkip;
} else {
drawRow(out, start, month, maxDays, 0);
start += 7;
}
}
int width = SIZE *7 ;
String str = "";
for (int i = 0; i <= width; i++)
str += HORIZONTAL;
out.print(str);
out.println();
}
public static void drawRow(PrintStream out, int start, int month, int maxDaysInMonth, int skip) {
String str = "";
int width = SIZE * 7;
for (int i = 0; i <= width; i++)
str += HORIZONTAL;
out.println(str);
int day = start;
String space = " ";
out.print("|");
for (int cell = 1; cell <= 7; cell++) {
str = "";
if (cell > skip && day <= maxDaysInMonth) {
if(dayValue == day) {
str += "*" + day++ + "*";
} else {
str += day++;
}
}
while (str.length() < SIZE - 1)
str += space;
str += VERTICAL;
out.print(str);
}
int height = SIZE / 2;
day = start;
for (int h = 2 ; h <= height; h++) {
out.print(" |");
for (int cell = 1; cell <= 7; cell++, day++) {
str = "";
if(h == 2 && cell > skip && day <= maxDaysInMonth && eventArray[month-1][day-1] != null)
str = eventArray[month-1][day-1];
while (str.length() < SIZE - 1)
str += space;
str += VERTICAL;
out.print(str);
}
}
out.print(" ");
}
public static void displayDate(PrintStream out, int month, int day) {
if (showLine == true) {
out.println("Month: " + month);
out.println("Day: " + day);
}
}
public static int monthFromDate(String date) {
String tokens[] = date.split("/");
return Integer.parseInt(tokens[0]);
}
public static int dayFromDate(String date) {
String tokens[] = date.split("/");
return Integer.parseInt(tokens[1]);
}
public static void drawCalendar(PrintStream out, int month, int day) {
int startingDay = getStartingDay(month);
int maxDays = getMaximumDays(month);
drawMonth(out, month, startingDay, maxDays);
displayDate(out, month, day);
}
public static int nextGoodMonth(int month) {
int mt = month;
if (month == 12)
return 1;
return mt + 1;
}
public static int previousGoodMonth(int month) {
int mt = month;
if (month == 1)
return 12;
return mt - 1;
}
public static int getStartingDay(int month) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.MONTH, month - 1);
return cal.get(Calendar.DAY_OF_WEEK) - 1;
}
public static int getMaximumDays(int month) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, month - 1);
return cal.getActualMaximum(Calendar.DATE);
}
public static void main(String[] args) {
Scanner keybd = new Scanner(System.in);
String cmd = "";
int day = -1;
int month = -1;
loadEventsFromFile("calendarEvents.txt"); //load events at start up
while (true) {
System.out.println("Please type a command");
System.out.println("\t\"e\" to enter a date and display the corresponding calendar.");
System.out.println("\t\"ev\" to enter an event.");
System.out.println("\t\"fp\" to print calendar to file");
System.out.println("\t\"t\" to get todays date and display the todays calendar");
System.out.println("\t\"n\" to display the next month");
System.out.println("\t\"p\" to display the previous month");
System.out.println("\t\"q\" to quit the progrram");
cmd = keybd.nextLine();
if (cmd.equalsIgnoreCase("e")) {
// get user input date
System.out.print("Enter a date (m/d): ");
String date = keybd.nextLine();
dayValue = day = dayFromDate(date);
month = monthFromDate(date);
showLine = true;
drawCalendar(System.out, month, day);
continue;
}
else if (cmd.equalsIgnoreCase("ev")) {
// get user input date
System.out.print("Enter an event (m/d event_title): ");
String date = keybd.next();
String event = keybd.nextLine();
int d = dayFromDate(date);
int m = monthFromDate(date);
eventArray[m-1][d-1] = event;
if (d == 1 || m == 1) {
eventArray[m][d] = event;
}
}
else if (cmd.equalsIgnoreCase("t")) {
Calendar cal = Calendar.getInstance();
dayValue = day = cal.get(Calendar.DATE);
month = cal.get(Calendar.MONTH) + 1;
System.out.println("month" + "/" + "day");
showLine = true;
drawCalendar(System.out, month, day);
}
else if (cmd.equalsIgnoreCase("n")) {
if (day != -1 && month != -1) {
day = 1;
dayValue = -1; // as user has not mentioned any value specifically
month = nextGoodMonth(month);
System.out.println(month);
showLine = false;
drawCalendar(System.out, month, day);
continue;
}
else {
System.out.println("Please display a calendar first. ");
continue;
}
}
else if (cmd.equalsIgnoreCase("p")) {
if (day != -1 && month != -1) {
day = 1;
// as user has not mentioned any value specifically
dayValue = -1;
month = previousGoodMonth(month);
System.out.println(month);
showLine = false;
drawCalendar(System.out, month, day);
continue;
}
else {
System.out.println("Please display a calendar first. ");
continue;
}
}
else if (cmd.equalsIgnoreCase("fp")) {
System.out.println("Enter month to print (1-12):");
month = keybd.nextInt();
System.out.println("Enter filename: ");
String fname = keybd.next().trim();
keybd.nextLine();
dayValue = -1;
try {
PrintStream outfile = new PrintStream(new File(fname));
drawCalendar(outfile, month, day);
outfile.close();
}
catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
else if (cmd.equalsIgnoreCase("q")) {
System.out.println("Program terminated...");
break;
}
else {
System.out.println("Please enter a valid command. ");
continue;
}
}
keybd.close();
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started