Hi. I've been working on this simple agenda-styled program for quite some time. I've went through it several times, and at runtime I get an ArrayOutOfBoundsException: 2, even though I've checked all of my index numbers and references several times, and they should all be working properly. My hash code also could have something to do with it, but I've checked that too! Here's the method along with class-declared variables and the method for my hash code:
private String addOcc = "What is the name of this " + "occasion?";
private String addDate = "What is the date of"
+ " this occasion? (Numbers and / or - only.)";
private StringTokenizer st;
private String[][] calendar = new String[100][2];
public void addDate () {
String date =
JOptionPane.showInputDialog(null, addDate);
String occasion =
JOptionPane.showInputDialog(null, addOcc);
int arrayIndexNo =
hash(date);
date = calendar[arrayIndexNo][0];
occasion = calendar[arrayIndexNo][1];
JOptionPane.showMessageDialog(null, "Date added.");
call(); //Calls the main method
public int hash (String date) {
st = new StringTokenizer(date, "-/ ");
String str = "";
while (st.hasMoreTokens()) str += st.nextToken();
int last = str.length() - 3;
String x = str.charAt(0) + str.charAt(1) + str.charAt(last);
Integer d = Integer.parseInt(x);
int hash = (d - 110) % 10;
int arrayIndexNo = hash;
return arrayIndexNo;
}
If you need the rest of the class, or the class with the main method, just ask. Thank you so much.