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.");

        
    }
}
Comments
Code tags on first post + you've made a huge effort, this definitely deserves some rep :)

two things.

one you are hard coding lastDay[11], you may know this and its for debugging.

the second is you are setting valid twice.

First you set valid to true or false on the day issue. But then you set it again wiping out any previous answer it had on the month.

i.e. day not true so valid false.
then month is true so make valid true.

what you need to do is not perform the second check if valid is allready false, because you are giving them a second chance to make it true.

Mike

Comments
Good post :)

i might also check month first so you dont check day if month is an illegal answer i.e .month 30.

As said before, you should check the month before the day because February the 30th is not valid.

try something like this

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);
if (month>=1 && month<=12)
{
    if (day>0 && day<=lastDay[month-1]) valid=true;
    else valid=false;
}
else valid=false;
}while (!valid)

Moreover, if you want to handle leap years, you should check whether the year is divisible by 4 and not divisible by 400.


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.");

        
    }
}

As said before, you should check the month before the day because February the 30th is not valid.

Of course you should, and just for the sake of clarity, if you don't, then you risk an array boundary overrun (imagine that [B]month[/B] is bigger than 12).

Also,

if (month>=1 && month<=12)
{
    if (day>0 && day<=lastDay[month-1]) valid=true;
    else valid=false;
}
else valid=false;

can be replaced by the (IMO) much easier to understand:

if ( month < 0 || month > 12 )
  valid = false;

if ( day < 0 || day > lastDay[ month - 1 ] )
  valid = false;

:)

Edited 6 Years Ago by mvmalderen: n/a

if ( month < 0 || month > 12 )
  valid = false;

if ( day < 0 || day > lastDay[ month - 1 ] )
  valid = false;

Small bug fix of my own code:

if ( month <= 0 || month > 12 )
  valid = false;

if ( day <= 0 || day > lastDay[ month - 1 ] )
  valid = false;

Everything should be 100% correct now :P

Hey lads,

thanks for all of the replies. I eventually got it right after messing around with it, not before it had driven me demented. heres what I did.

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};
        month = lastDay.length;
        boolean valid = true;
        
        // prompt user input

        // sort out months

        do {

        String input2 = JOptionPane.showInputDialog("Please enter month here:");
        month = Integer.parseInt (input2);

        if (month > 0  &&  month <= lastDay.length) {
            valid = true;

        }
        else valid = false;
        
        }
        while(!valid);

        // sort out years

        do {

        String input3 = JOptionPane.showInputDialog(" Please enter year here:");
        year = Integer.parseInt(input3);

        if (year > 1000 && year < 10000){
            valid = true;
        }
        else valid = false;
        }
        while(!valid);
 
        // sort out days

        do {

        String input1 = JOptionPane.showInputDialog("Please enter date here:");
        day = Integer.parseInt (input1);

        if (day > 0  && day <= lastDay[month-1]) {
	   valid = true;

        }
        else valid = false;

        if (month == 02 && day == 29 && year % 4 == 00) {
            valid = true;
            System.out.println(" It's a leap day.");
        }
        
        }
        while (!valid);

        // Finish

        System.out.println(" The Date is: " + day + "/" + month + "/"
                + year + ".");

     }

 }

Thanks again for the replies.

Hey Bobon,

If you precede an integer with a zero, then it turns into an octal number, so you should change:

if (month == 02 && day == 29 && year % 4 == 00) {
  valid = true;
  System.out.println(" It's a leap day.");
}

to this:

if (month == 2 && day == 29 && year % 4 == 0) {
  valid = true;
  System.out.println(" It's a leap day.");
}

Also you might want to enhance your code to display a date, this is what you have now:

System.out.println(" The Date is: " + day + "/" + month + "/"
                + year + ".");

, say you have a date like this:

day = 9
month = 1
year = 2010

then your program will display it as: 9/1/2010 but with a slight effort, you could make it display as: 09/01/2010 (which looks more nice IMO)
just a suggestion though :)

This article has been dead for over six months. Start a new discussion instead.