Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •