Capattax Capattax - 1 year ago 178
Java Question

How to check if card has expired in java program?

So I am making a Java program that processes a user's fake credit card and I am trying to make an expiration verification conditional statement. However, when I run my program, I get a "String cannot be converted to int" error. I would like to know how should I use the current month and year to check if the date entered by the user has actually expired.

do {
System.out.print("Enter the expiration date mmyy: ");
expiration = expnum.nextInt();

DateFormat dateformat = new SimpleDateFormat("MMyy");
Date date = new Date();
int currentdate = dateformat.format(date);

if (currentdate <= expiration)
check = check + 1;
check = 1;
} while (check == 1);

Answer Source

It's not an int. It's a string. This is wrong:

int currentdate = dateformat.format(date);

If you want an int, then convert it to an int:

int currentdate = Integer.parseInt(dateformat.format(date));

It won't be a terribly sensible int though.

You should make that variable check a Boolean value. You have no need for it to be an numeric value.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download