Hey,
I've been going insane with a question I've been doing all day.
Creating an application that prompts a user to enter a date.
I have to use arrays to store the number of days in each month i.e 31 for January, 28 for February etc..
I have created a loop so that if someone entered incorrectly, the "33rd of the 5th month" or the 12th of the 20th month, that the user will be prompted to re-enter the date.
When I run the programme it will re-prompt if the user enters a wrong month but not the date. If for example I enter 35 for the day, it will go ahead instead of returning to loop and re-prompting.
Now I have to store the days of the months in an array as that is part of the question.
I was wondering if anyone could help me to get the app. to re-prompt when the wrong day is entered? I have a feeling the arrays are the problem.
import javax.swing.*;
class partbq1a {
public static void main(String[] args) {
int day;
int month;
int year;
int[] lastDay = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
boolean valid = true;
// prompt user input
do {
String input1 = JOptionPane.showInputDialog("Enter Date in number format here:");
day = Integer.parseInt (input1);
String input2 = JOptionPane.showInputDialog("Enter Month in number format here:");
month = Integer.parseInt (input2);
// sort out days
if (day > 0 && day <= lastDay[11])
valid = true;
else valid = false;
// sort out months here
if (month >= 1 && month <= 12)
valid = true;
else valid = false;
}
while (!valid);
String input3 = JOptionPane.showInputDialog(" Enter Year in number format here:");
year = Integer.parseInt(input3);
System.out.println(" The Date is: " + day + "day, " + month + "month, "
+ year + "year.");
}
}