8 Replies - 638 Views - Last Post: 19 September 2011 - 03:36 PM Rate Topic: -----

#1 c-law  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-September 11

program to determine a valid date

Posted 19 September 2011 - 09:14 AM

So i need to write a program in which determines if a date entered as mm/dd/yyyy is a valid date.
I am floundering and would love any ideas

Here's my code so far....



import java.util.*;


	public class validDate {
		public static void main (String[] args){
		
		Scanner in;
		in = new Scanner (System.in);
		System.out.println("Enter the date as: mm/dd/yyyy");
		in.useDelimiter("/");
		
		double m;
		double d;
		double y;
		
		m = in.nextDouble();
		d = in.nextDouble();
		y = in.nextDouble();
	
			double invalidM = 0;
			double invalidD = 0;
			double invalidY = 0;
		
			
			
		switch (m ){
			
			case 1:
				m = (1, 3, 4, 7, 8, 10, 12) ;
				//days allowed 31
				break;
			case 2: 
				m = (4, 6, 9, 11) ;
				//days allowed 30
				break; 
			case 3: 
				m = (2) ;
				//days allowed 28, unless leapYear then 29
				break;
		}
		
		{
		boolean leapYear = true;

			if(y %400 == 0){
				leapYear = true;  }
			if(y % 100 == 0){
				leapYear = false; }
			if (y % 4 == 0){
				leapYear = true;  }
			else { 
				leapYear = false; }
		  }

		
			//months w/ 31 days
			//if ( (m = 1) || (m = 3) || (m = 4) || (m = 7) 
			//		|| (m = 8) || (m = 10) || (m = 12))		{
			//	(1 <= days <= 31);}
			//months w/ 30 days
			//if ( (m = 4) || (m = 6) || (m = 9) || (m = 11)) {
			//	(1 <= days <= 30)}
			//FEB
			//if ( (m = 2) ){
				
			}
		
		}



Is This A Good Question/Topic? 0
  • +

Replies To: program to determine a valid date

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: program to determine a valid date

Posted 19 September 2011 - 09:18 AM

Please let us know what you need help with. What are you having trouble with? Why do you think you're floundering? Is something wrong with the code? Are you getting error messages? If you are getting errors, include them in your posts, cut and paste.
Was This Post Helpful? 0
  • +
  • -

#3 c-law  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-September 11

Re: program to determine a valid date

Posted 19 September 2011 - 09:36 AM

I just don't know where to go from here; within the switch statement it says "cannot switch value type Double."
should I write another if statement to check the days of each month and how do i interpret the input as separate
values when they are divided by a "/"

View Postc-law, on 19 September 2011 - 09:32 AM, said:

I just don't know where to go from here; within the switch statement it says "cannot switch value type Double."
should I write another if statement to check the days of each month and how do i interpret the input as separate
values when they are divided by a "/"

P.S. within the switch statement those comments are what I need to compare those months to with that amount of days. should this be written in a different if statement?
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: program to determine a valid date

Posted 19 September 2011 - 10:15 AM

Quote

cannot switch value type Double.

Is telling you that your switch value cannot be of type double. Here's the switch tutorial so that you can review the switch statement and modify your code to use a switch value that is an allowed type.

Quote

should I write another if statement to check the days of each month

You could use an 'if' statement (or multiple 'if' statements), but your switch statement will work if you fix it. You didn't write it correctly, so review that tutorial, focusing on the structure and how it works. What you have written is broken but easily fixable.

Quote

how do i interpret the input as separate values when they are divided by "/"

Your definition of the Scanner instance called "in" should be taking care of that for you, but describe any problems you're having with that.

Quote

within the switch statement those comments are what I need to compare those months to with that amount of days. should this be written in a different if statement?

You just need to fix your switch statement.

This post has been edited by GregBrannon: 19 September 2011 - 10:16 AM

Was This Post Helpful? 0
  • +
  • -

#5 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: program to determine a valid date

Posted 19 September 2011 - 10:34 AM

As GregBrannon has said, all you need to do is change your value type to the respected type.
1. A switch method takes int's, well you have all of yours as a double.
2. Your month day and year values are doubles,you cannot have 1.3 parts of a day

You need to change your value types to the respected int's they should be. Also need to change your method to appropriate the int's instead of doubles.
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8009
  • View blog
  • Posts: 13,716
  • Joined: 19-March 11

Re: program to determine a valid date

Posted 19 September 2011 - 11:21 AM

1) I'm not aware of any calendar system that uses floating-point dates. Presumably an int would do the trick.


2) Since you're looking up an int value based on an int value, and the keys are unique and not sparse, you might consider using an array lookup instead of a switch. (unique: the days in a month are a function of the month, each month has exactly one days value in a given year. "not sparse": the keys occupy the whole of the range from 1 to 12. Since an array's size depends on the value from lowest to highest key, a sparse array would be, for example, an array with keys 3, 5, 7, 11, 13, 17, 19, 23. This would be very inefficient in terms of space: only one in three of the spaces allocated would actually be used)
Was This Post Helpful? 0
  • +
  • -

#7 cmpshr  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 120
  • Joined: 22-August 11

Re: program to determine a valid date

Posted 19 September 2011 - 02:20 PM

    int nbDays(int month, int year) {
       int[]  days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
       if(isLeapYear(year))
          days[2] = 29;
       return days[month];
    }


Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8009
  • View blog
  • Posts: 13,716
  • Joined: 19-March 11

Re: program to determine a valid date

Posted 19 September 2011 - 02:29 PM

Yes, cmpshr, that's about what I was suggesting that c-law might try to write. Congratulations, you're clever. Now, do you suspect there was a reason I didn't just write that? Do you think that c-law might have benefitted somewhat from having had to put that together himself?
Please try to let others think for themselves, you help nobody by writing this stuff out for them.
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,993
  • Joined: 20-September 08

Re: program to determine a valid date

Posted 19 September 2011 - 03:36 PM

What about the following?

	DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
	df.setLenient(false);
        df.parse("13/12/2011");


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1