Black-Cell.net

  • June 22, 2018, 12:06:45 PM
  • Welcome, Guest
Please login or register.

Login with username, password and session length
Advanced search  

News:

Pages: [1]   Go Down

Author Topic: Anyone know java?  (Read 1262 times)

KobraOps

  • Renegade Server VIP
  • Offline Offline
  • Posts: 490
    • View Profile
Anyone know java?
« on: October 20, 2007, 10:46:42 PM »

import java.util.Scanner;
 
 public class DateValid
 {
    public static void main (String [] args )
    {
       int month, day, year;
       int daysInMonth;
       boolean monthValid, yearValid, dayValid, leapYear;
       Scanner scan = new Scanner(System.in);
       
       //Date from user
       
       System.out.println ("What year is it?");
       year = scan.nextInt();
       System.out.println ("Month?(number)");
       month = scan.nextInt();
       System.out.println ("Day?(number)");
       day = scan.nextInt();
       
       //Determining if month is valid
       
       if (month <= 12 && month != 0)
       {
          monthValid = true;
       }
       else
       {
          monthValid = false;
       }
       //Determining if year is valid
       
       if(year > 1000 && year < 1999)
       {
          yearValid = true;
       }
       else
       {
          yearValid = false;
       }
        //Determining if its a leap year
        if (year % 4 == 0)
        {
           if(year % 100 == 0)
           {
              if (year % 400 == 0)
              {
                 leapYear = true;
              }
              else
              {
              leapYear = false;
              }   
              
           }
        else
        {
           leapYear = true;
        }
       }
       
        //Determines days in the month   
        if(month == 1 || month == 3 ||  month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
        {   
           if (day <= 31)
           {
              dayValid = true;
           }
        }
        else
        {
           daysInMonth = 0;
        }
       if(month == 4 || month == 6 || month == 9 || month == 11)
       {
          if (day <= 30)
          {
             dayValid = true;
          }
       }
       else
       {
          daysInMonth = 0;
       }
       if(leapYear = true)
       {
          if (month == 2)
          {
             if (day <= 29)
             {
                dayValid = true;
             }      
                else
                {
                   daysInMonth = 0;
                }
          }
       }
       if (daysInMonth == 0)
       {
          monthValid = false;
       }
       else
       {
          monthValid = true;
       }
       
       if (dayValid = true && monthValid == true && yearValid == true)
       {
          System.out.println("Date is valid");
          {
          if(leapYear = true)
             System.out.println("It's a leap year");
          }
       }
       else
       {
          System.out.println("Date is not valid");
       }
       
          
    }
 }

Well ive just started using java (in a school class) and the purpose of this program is to determine if  a date isvalid
When i compile i keep gettin an error saying that "daysInMonth" might not have been initilized, but i dont see anything wrong >.>

Anyone?
Logged
"I had a girlfriend once" - Prplhazed

KobraOps

  • Renegade Server VIP
  • Offline Offline
  • Posts: 490
    • View Profile
Re: Anyone know java?
« Reply #1 on: October 20, 2007, 10:48:12 PM »

if (daysInMonth == 0)
       {
          monthValid = false;
       }
       else
       {
          monthValid = true;
       }
 
srry i mean here ^
Logged
"I had a girlfriend once" - Prplhazed

KobraOps

  • Renegade Server VIP
  • Offline Offline
  • Posts: 490
    • View Profile
Re: Anyone know java?
« Reply #2 on: October 20, 2007, 11:16:34 PM »

nvm.
Logged
"I had a girlfriend once" - Prplhazed

vloktboky

  • Offline Offline
  • Posts: 2631
    • View Profile
Re: Anyone know java?
« Reply #3 on: October 21, 2007, 06:52:03 AM »

Initialization means performing an assignment on the same line as the declaration. The terminology is universal:

Declaration: int a;
Assignment: a = 5;
Initialization: int a = 5;

You declared "daysInMonth" at the beginning. All possible assignments are inside conditional blocks (if/else statements). But you perform an equality check always on it. The compiler is simply letting you know that you may be performing an equality check on a variable that has no value assigned to it, if none of those assignments are hit. It's telling you that to avoid any such logical bug, you should initialize it to a value so the equality check will always have something valid to work with, irregardless if those nested assignments are hit or not.
Logged
Pages: [1]   Go Up