Posted by:aska0865
Posted on:2004-02-14 12:16
i just did the dateException lab, it's not the one you want but that may help someone who want to learn how to check a correct mmddyyy format
public class DateException extends Exception{
public DateException (){
super("Invalid Date");
}
public DateException (String message){
super(message);
}
public DateException(int mon, int day){
super("Month"+String.valueOf(mon)+" has only has "+String.valueOf(day)+"days. Try again.");
}
// Java core package
import java.awt.*;
import java.awt.event.*;
//Java extension pckages
import javax.swing.*;
public class ExceptionCal extends JFrame implements ActionListener{
private JTextField textField;
private JButton button;
private String date="",d;
private int mon,day,year;
private int daysOfMon[]={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
private int daysOfMonLeap[]={0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
//set up GUI
public ExceptionCal(){
super("Enter your birth day");
//get content pane and set its layout
Container container = getContentPane();
container.setLayout(new FlowLayout());
//set up the text field and the button
textField = new JTextField(10);
button = new JButton("Enter");
//register listener to button
container.add(textField);
container .add(button);
button.addActionListener(this);
setSize(320,240);
setVisible(true);
}
// process GUI events
public void actionPerformed(ActionEvent event){
//check events
if(event.getSource()==button){
//call getDate method
getDate();
}
}
//method getDate from the text field
private void getDate(){
//get the string from test field
d=textField.getText();
try{
// read string to mon,day and year
mon=Integer.parseInt(d.substring(0,2));
day=Integer.parseInt(d.substring(2, 4));
year= Integer.parseInt(d.substring(4,8));
// check the valid date
checkDate();
}
//process improperly length input
catch(StringIndexOutOfBoundsException outOfBound){
JOptionPane.showMessageDialog(null,"invalid length of date");
}
//process improperly type input
catch(NumberFormatException formatExceptionCal){
JOptionPane.showMessageDialog(null,"invalid data format");
}
//process improperly formatted input
catch(DateException dataException){
JOptionPane.showMessageDialog(this, dataException.toString());
}
}
//method checkDate throwing exceptions
//when invalid date format occurs
private void checkDate()throws DateException{
//check the date length
if(d.length()!=8)throw new DateException("invalid Date Format");
//check the month
if(mon<1||mon>12)throw new DateException("Month only between 1-12");
//check the leap years
if(year%4==0){
if(day<0||day>daysOfMonLeap[mon]) throw new DateException(mon,daysOfMonLeap[mon]);
}
//check the rest of years
else if(day<0||day>daysOfMon[mon]) throw new DateException(mon,daysOfMon[mon]);
//dispaly the valid date
JOptionPane.showMessageDialog(null,
"day of birth: "+day+"\n month of birth "+mon+"\n year of birth:"+year);
}
//main method
public static void main(String[] args) {
ExceptionCal test= new ExceptionCal();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} // end main
}//end class