By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,554 Members | 1,146 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,554 IT Pros & Developers. It's quick & easy.

Leap year calculater problems, validating days

P: 14
Ok, I am making a program (java class file) 2 work out if a date entered is valid or invalid for the day, month year, and for leap years.. dd/mm/yyyy .
I seem 2 have gotten it 2 validate the year and month, but my code errors on the day validation.

Error(91,38): variable daysInMonth might not have been initialized.

Any thoughts, all help welcome. I only ask as a last resort as it is frustrating me 2 pulling my hair out.





Expand|Select|Wrap|Line Numbers
  1. import java.io.*; 
  2.  
  3. public class Leapyear 
  4.    public static void main(String[] args) throws IOException{ 
  5.        int day, month, year,  daysInMonth; 
  6.        String leap,monthValid, dayValid, yearValid, leapYear; 
  7.  
  8.  
  9.  
  10.                { 
  11.  
  12.  
  13.                              year = Text.readInt("please enter your year"); 
  14.  
  15.                             if(year >=0000) 
  16.                           { 
  17.                                   yearValid = "yes"; 
  18.                           } 
  19.                           else 
  20.                           { 
  21.                                   yearValid = "no"; 
  22.                           } 
  23.                        if (year % 400 == 0) 
  24.                        { 
  25.                        leap = "yes"; 
  26.                                         } 
  27.                        else if (year % 100 == 0) 
  28.                        { 
  29.                           leap = "no"; 
  30.                                   } 
  31.                     else if (year % 4 == 0) 
  32.                     { 
  33.                          leap = "yes"; } 
  34.                    else 
  35.                           {leap = "no"; 
  36.                           } 
  37.                } 
  38.  
  39.        month = Text.readInt("Type in a numeric month"); 
  40.  
  41.            { 
  42.                    switch (month) 
  43.                    { 
  44.                    case 1: daysInMonth = 31; 
  45.                    break; 
  46.                    case 3: daysInMonth = 31; 
  47.                       break; 
  48.                    case 4: daysInMonth = 30; 
  49.                       break; 
  50.                    case 5: daysInMonth = 31; 
  51.                       break; 
  52.                    case 6: daysInMonth = 30; 
  53.                       break; 
  54.                    case 7: daysInMonth = 31; 
  55.                       break; 
  56.                    case 8: daysInMonth = 31; 
  57.                       break; 
  58.                    case 9: daysInMonth = 30; 
  59.                       break; 
  60.                    case 10: daysInMonth = 31; 
  61.                       break; 
  62.                    case 11: daysInMonth = 30; 
  63.                       break; 
  64.                    case 12: daysInMonth = 31; 
  65.                   break;                    } 
  66.  
  67.                  if (month >=1 && month <=12) 
  68.                  { 
  69.                        monthValid = "yes"; 
  70.                  } 
  71.                  else 
  72.                  { 
  73.                        monthValid = "no"; 
  74.                  } 
  75.                if (month == 2) 
  76.                { 
  77.                      if(leap == "no") 
  78.                      { 
  79.                          daysInMonth = 28; 
  80.                      } 
  81.                      else if (leap == "yes") 
  82.                      { 
  83.                          daysInMonth = 29; 
  84.                      } 
  85.                } 
  86.  
  87.        day = Text.readInt("Type in the day number"); 
  88.  
  89.  
  90.              if ( 1 <= day && day <= daysInMonth ) 
  91.              { 
  92.                  dayValid = "yes"; 
  93.              } 
  94.  
  95.              else 
  96.              { 
  97.                  dayValid = "no"; 
  98.              }  // this is where the problem seems 2 be
  99.  
  100.  
  101.  
  102.        if ((dayValid == "yes") && (monthValid == "yes") && (yearValid == "yes")) 
  103.                { 
  104.                      System.out.println ( day + "/" + month + "/" + year + " - valid"); 
  105.                } 
  106.                else 
  107.                { 
  108.                      System.out.println ( day + "/" + month + "/" + year + " - invalid"); 
  109.                } 
  110.  
  111.        } 
  112.  
  113.    } 
  114.  
  115.  
  116.    //end main
  117.  

All thoughts welcome.
Dec 6 '06 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 1,510
you don't initialise daysInMonth in the statement
Expand|Select|Wrap|Line Numbers
  1.        int day, month, year,  daysInMonth; 
  2.  
if in your switch(month) statement month is not in the range 1 to 12 daysInMonth will not be assigned a value so when the statement
Expand|Select|Wrap|Line Numbers
  1.    if ( 1 <= day && day <= daysInMonth ) 
  2.  
is executed daysInMonth would not have a value - hence the compiler error.

you either initialise daysInMonth in
Expand|Select|Wrap|Line Numbers
  1.        int day, month, year,  daysInMonth=0; 
  2.  
or put a default statement in the switch(), e.g.
Expand|Select|Wrap|Line Numbers
  1.    default: daysInMonth=0;
  2.  
Dec 6 '06 #2

P: 14
owz
thanx very much, cant beleive its so simple. you where a great help.
Dec 6 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.