Can't believe it has gotten to this. I cannot for the life of me figure out what is messing up my desired outcome. Here is the story:
I have a small GUI program for a sports club. I have 3 list boxes, 1 for each U10's,U12's and U14 children. I have a textfield to enter name. 3 Combo boxes for the dob, and a radio button for gender.
My problem is, When a child is in the U10 age group, it adds that child to all 3 list boxes, but it works fine on both U12 and U14's. It is coded exactly the same way as the other 2. I tried switching the order of execution to test if it happened to the others, but it just continued to happen to the U10's. This is bugging me. I will post the code I think will be helpful.
Classify Button - adds info to the list boxes
private void m_btnClassifyActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String wrongAge = "candidate must be between 8 and 15 years old!";
if(doValidate())
{
if(checkAge()==10)
{
fillList(10);
clearFields();
}
else if(checkAge()==12)
{
fillList(12);
clearFields();
}
else if(checkAge()==14)
{
fillList(14);
clearFields();
}
else{
JOptionPane.showMessageDialog(this, wrongAge,"Invalid Entry", JOptionPane.ERROR_MESSAGE);
}
}
}
validation method
private boolean doValidate()
{
String notEnoughMsg = "You must enter a day,month and year!";
String noName = "You must enter a name";
if(m_cbDay.getSelectedIndex()==0 || m_cbMonth.getSelectedIndex()==0 || m_cbYear.getSelectedIndex()==0)
{
JOptionPane.showMessageDialog(this, notEnoughMsg,"Invalid Entry", JOptionPane.ERROR_MESSAGE);
return false;
}
else if(m_tfName.getText().equals(""))
{
JOptionPane.showMessageDialog(this, noName,"Invalid Entry", JOptionPane.ERROR_MESSAGE);
return false;
}
return true;
}
age calculation
private int checkAge()
{
Date today = Calendar.getInstance().getTime();
long todayMs = today.getTime();
int year = Integer.parseInt((String)m_cbYear.getSelectedItem());
int month = Integer.parseInt((String)m_cbMonth.getSelectedItem());
int day = Integer.parseInt((String)m_cbDay.getSelectedItem());
GregorianCalendar calAge = new GregorianCalendar(year,month-1,day);
Date newAge = calAge.getTime();
long ageMs = newAge.getTime();
long elapsedTime = todayMs - ageMs;
int age = (int)(elapsedTime / 1000 / 24 / 60 / 60 / 365);
if(age < 7 || age > 15)
return -1;
else if(age > 7 && age <= 10)
return 10;
else if(age > 10 && age <=12)
return 12;
else if(age > 12 && age <=15)
return 14;
else
return 0;
}
fillList method- adds contents to the list
private void fillList(int age)
{
String gender = "";
String name = m_tfName.getText();
if(m_rbMale.isSelected())
gender = m_rbMale.getText();
else
gender = m_rbFemale.getText();
if(age==10)
{
m_lbU10.setModel(defaultListModel1);
defaultListModel1.addElement("Name: "+name);
defaultListModel1.addElement("Gender: "+gender);
}
else if(age==12)
{
m_lbU12.setModel(defaultListModel2);
defaultListModel2.addElement("Name: "+name);
defaultListModel2.addElement("Gender: "+gender);
}
else{
m_lbU14.setModel(defaultListModel3);
defaultListModel3.addElement("Name: "+name);
defaultListModel3.addElement("Gender: "+gender);
}
}
Any help is appreciated.