- #1
stripes
- 266
- 0
Homework Statement
I am making a very, very simple program that takes in the date (of type string) from the user in the form DD/MM/YYYY. It then confirms whether or not the date is legitimate or not. For example, 09/07/2012 is valid, but 09/17/2012 is not valid.
My program compiles correctly, but no matter what input I give it, the program always generates an exception. Here is my code:
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
public class VerifyDate
{
//static final int SLASH_1_INDEX = 2;
//static final int SLASH_2_INDEX = 5;
static final int JAN = 1;
static final int FEB = 2;
static final int MAR = 3;
static final int APR = 4;
static final int MAY = 5;
static final int JUN = 6;
static final int JUL = 7;
static final int AUG = 8;
static final int SEP = 9;
static final int OCT = 10;
static final int NOV = 11;
static final int DEC = 12;
static String date;
static final int DAY_MAX = 31;
static final int FEB_MAX = 28;
//static int SLASH_1_INDEX = date.indexOf("/");
//static int SLASH_2_INDEX = date.lastIndexOf("/");
public static void main(String[] args)
{
JFrame frame = new JFrame();
JPanel panel = new JPanel(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
final int FRAME_WIDTH = 450;
final int FRAME_HEIGHT = 450;
JTextField inputDate;
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("Verify a date");
JOptionPane.showMessageDialog(null,
"This program verifies if the input is a correct date." +
" \nPress OK to continue.");
JLabel label = new JLabel("Enter date (DD/MM/YYYY): ");
inputDate = new JTextField(12);
date = inputDate.getText();
JButton b1 = new JButton("Verify date");
b1.setVerticalTextPosition(AbstractButton.CENTER);
b1.setHorizontalTextPosition(AbstractButton.LEADING);
b1.setActionCommand("verify");
b1.addActionListener(new Action());
panel.add(label);
panel.add(inputDate);
panel.add(b1);
frame.add(panel);
frame.setVisible(true);
}
static class Action implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
int SLASH_1_INDEX = date.indexOf("/");
int SLASH_2_INDEX = date.lastIndexOf("/");
int day = Integer.parseInt(date.substring(0, SLASH_1_INDEX));
int month = Integer.parseInt(date.substring(SLASH_1_INDEX, SLASH_2_INDEX));
int year = Integer.parseInt(date.substring(SLASH_2_INDEX));
if (day <= 0 || day > DAY_MAX || month <= 0 || month > DEC || year <= 0)
JOptionPane.showMessageDialog(null, "The date specified is not valid");
else if ((month == APR || month == JUN || month == SEP || month == NOV) && day == DAY_MAX)
JOptionPane.showMessageDialog(null, "The date specified is not valid");
else if (month == FEB && day >= FEB_MAX)
JOptionPane.showMessageDialog(null, "The date specified is not valid");
else
JOptionPane.showMessageDialog(null, date + " is a valid date.");
}
}
}
and here is the error:
Code:
Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(Unknown Source)
at VerifyDate$Action.actionPerformed(VerifyDate.java:80)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
This is line 80 (the line that the exception refers to):
Code:
int day = Integer.parseInt(date.substring(0, SLASH_1_INDEX));
Why will my program refuse to do what it is supposed to? I tried putting '1', '2', and '3' as the index in question but to no avail.
Homework Equations
None.
The Attempt at a Solution
See above.