CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Java and JSP (http://www.codingforums.com/forumdisplay.php?f=54)
-   -   Help with some code. (http://www.codingforums.com/showthread.php?t=288621)

fanuv007 03-01-2013 08:46 PM

Help with some code.
 
We have an assignment for class, and my code compiles fine, but only runs correctly when I have a "+1" near the end of this line of code.

dayValid = (day >= 1 && day <= daysInMonth+1);

I will post the exact instructions, though with my comments and organization it should be apparent to anyone with experience what is the issue.

Any help or explanation will be greatly appreciated!

Thank you.

Code:


// ************************************************************
//
// Dates.java                       
//
// Determine whether a 2nd-millenium date entered by the user
// is valid
//
// ************************************************************

import java.util.Scanner;
       
public class Dates

{

        public static void main(String[] args)
       
        {

                //------------------------------------------------
                //
                // Variables
                //
                //------------------------------------------------
               
                int month, day, year; //date read in from user
               
                int daysInMonth; //number of days in month read in
               
          boolean monthValid, yearValid, dayValid; //true if input from user is valid
               
                boolean leapYear; //true if user's year is a leap year
               
                boolean dateValid; //true if date is valid
               
               
                //-------------------------------------------------
                //
                // In this section we will have the user enter in variables
                //
                //-------------------------------------------------
               
                Scanner scan = new Scanner(System.in);
               
                //Get integer month, day, and year from user

                System.out.println ("Please enter the month as an integer, for example August would be 8: ");
                month = scan.nextInt();

                System.out.println ("Please enter the day of the month: ");
                day = scan.nextInt();
               
                System.out.println ("Please enter the year: ");
                year = scan.nextInt();
               
                //--------------------------------------------------
                //
                //        Validation check section
                //
                //--------------------------------------------------

                //Check to see if month is valid
               
                monthValid = (month >= 1 && month <= 12);
               
               
                //Check to see if year is valid
               
                yearValid = (year >= 1000 && year <= 1999);
               

                //Determine whether it's a leap year

                leapYear =((year % 4) == 0 && ((year % 100) != 0) || (year % 400) == 0);
               
               
                //Determine number of days in month
               
                if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12);
                daysInMonth = 31;
               
                if (month == 4 || month ==6 || month == 9 || month == 11);
                daysInMonth = 30;
               
                if (month == 2 && leapYear)
                daysInMonth = 29;
               
                if (month == 2)
                daysInMonth = 28;
               
                //---------------------------------------------------------
                //
                // The not valid month statement
                //
                //---------------------------------------------------------
               
                if (!monthValid)
                daysInMonth = 0;
               
                //User number of days in month to check to see if day is valid
               
                dayValid = (day >= 1 && day <= daysInMonth+1);
               
                //Determine whether date is valid and print appropriate message
               
                dateValid = (dayValid && monthValid && yearValid);
                       
               
                if        (!dateValid)       
               
                System.out.println ("The date you entered was invalid. Please try again");
                               
                       
                        else if (dateValid)
                        {
                       
                                if (leapYear)
                                {
                       
                                System.out.println ("The date you entered was valid and it's a LEAP YEAR!!!");
                               
                                }
                               
                                else
                               
                                System.out.println ("The date you entered was valid.");
                               
                                }
                                       
                               
        }
                       
               
}

In this exercise you will write a program that checks to see if a date entered by the user is a valid date in the second millenium. A skeleton of the program is in Dates.java. Save it to your directory. As indicated by the comments in the program, fill in the following:
1. An assignment statement that sets monthValid to true if the month entered is between 1 and 12, inclusive.
2. An assignment statement that sets yearValid to true if the year is between 1000 and 1999, inclusive.
3. An assignment statement that sets leap Year to true if the year is a leap year. The rule for a leap year can be found in the lecture slide.
4. An if statement that determines the number of days in the month entered and stores that value in variable daysInMonth. If the month entered is not valid, daysInMonth should get 0. Note that to figure out the number of days in February you’ll need to check if it’s a leap year.
5. An assignment statement that sets dayValid to true if the day entered is legal for the given month and year.
6. If the month, day, and year entered are all valid, print “Date is valid” and indicate whether or not it is a leap year. If any of the items entered is not valid, just print “Date is not valid” without any comment on leap year.

Fou-Lu 03-01-2013 11:17 PM

The problem looks to me that its not actually caused by the daysInMonth+1 as being a requirement, rather that daysInMonth is incorrect. Of course the daysInMonth + 1 would be invalid for something like april 31, which will check out as fine here. So you should just be checking for <= daysInMonth as you have assumed and that's correct.

Here's the problem:
Code:

                if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12);
                daysInMonth = 31;
               
                if (month == 4 || month ==6 || month == 9 || month == 11);
                daysInMonth = 30;

With the exception of february since it overwrites (although will still assign here as well), EVERY month is assigned daysInMonth = 30. That's caused by this:
Code:

if (month == 4 || month ==6 || month == 9 || month == 11);
                                                        ^

A semi-colon at the end of any control structure such as an if or a loop is true once and only once (and for the last option if its an iteration). So no matter what the month, even a month of -1, the daysInMonth will be 30 here, and the only exception is the 2nd month which then overwrites.

I'd recommend the use of braced evaluations as well as using elseif (since it can only be one of these possible options) or a switch.

fanuv007 03-02-2013 08:40 PM

Thank you so much for the detailed explanation. You made it really easy to understand what the problem was, and why.

I took your advice and used the switch method and got the program to work properly, and now I want to go back and try the other method too.

Again thank you!


All times are GMT +1. The time now is 12:36 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.