1

In an attempt to make my code a bit more aesthetic and short, I tried to change my code from this:

do
{
    System.out.print("Enter day: ");
    day = input.nextInt();

    if ((isLeapYear && month == 2 && (day < 1 || day > 29)) ||
            (!isLeapYear && month == 2 && (day < 1 || day > 28)) ||
            ((month == 4 || month == 6 || month == 9 || month == 11) && (day < 1 || day > 30)) ||
            ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 ||
            month == 10 || month ==12) && (day < 1 || day > 30)))
    {
        System.out.println("Invalid input");
    }
}
while ((isLeapYear && month == 2 && (day < 1 || day > 29)) ||
        (!isLeapYear && month == 2 && (day < 1 || day > 28)) ||
        ((month == 4 || month == 6 || month == 9 || month == 11) && (day < 1 || day > 30)) ||
        ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 ||
        month == 10 || month ==12) && (day < 1 || day > 30)));

to this:

boolean invalidDay = (isLeapYear && month == 2 && (day < 1 || day > 29)) ||
                    (!isLeapYear && month == 2 && (day < 1 || day > 28)) ||
                    ((month == 4 || month == 6 || month == 9 || month == 11) && (day < 1 || day > 30)) ||
                    ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 ||
                    month == 10 || month ==12) && (day < 1 || day > 30));

do
{
    System.out.print("Enter day: ");
    day = input.nextInt();

    if (invalidDay)
    {
        System.out.println("Invalid input");
    }
}
while (invalidDay);

The second one does not work as intended but the first way works perfectly. The only thing I have changed is putting the long boolean expression into a single boolean variable. Thanks.

FoxMcWeezer
  • 113
  • 1
  • 7

4 Answers4

1

invalidDay in your while() is never recalculated, you need to uptate its value somehow

morgano
  • 17,210
  • 10
  • 45
  • 56
  • Thanks a lot, it was just another dumb mistake caught by you guys. Obviously can't use day before asking the user. By moving the Boolean expression out and into its own thing and seeing it not work, I thought I was missing some underlying Java thing that I'd never be able to figure out – FoxMcWeezer Jun 30 '13 at 19:45
0

You are getting the value of day from console and checking in loop, by using boolean variable the check is not done each time but the previous value persists. If you want to do the same thing do it in a function and call it from inside the loop.

Ankit Zalani
  • 3,068
  • 5
  • 27
  • 47
0

You are missing an open bracket at the start of expression and the closing bracket at the end of expression ... It should be

boolean invalidDay = ((isLeapYear && month == 2 && (day < 1 || day > 29)) ||
                    (!isLeapYear && month == 2 && (day < 1 || day > 28)) ||
                    ((month == 4 || month == 6 || month == 9 || month == 11) && (day < 1 || day > 30)) ||
                    ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 ||
                    month == 10 || month ==12) && (day < 1 || day > 30)));
Mac
  • 1,711
  • 3
  • 12
  • 26
-1

In your while loop, the argument invalidDay is never updated. You could create a method for invalid day. Here:

public boolean invalidDay(int day) {
    return (isLeapYear && month == 2 && (day < 1 || day > 29)) ||
                (!isLeapYear && month == 2 && (day < 1 || day > 28)) ||
                ((month == 4 || month == 6 || month == 9 || month == 11) && (day < 1 || day > 30)) ||
                ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 ||
                month == 10 || month ==12) && (day < 1 || day > 30));
}

And now, in your while loop, use:

while(invalidDay(n));

Where n is the day.

Saket Mehta
  • 2,438
  • 2
  • 23
  • 28