SHARE
TWEET

Untitled

a guest Sep 22nd, 2019 106 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2. public class ValidDate
  3. {
  4.     public static void main(String[] args)
  5.     {
  6.         Scanner scanner = new Scanner(System.in);
  7.         System.out.print("enter a date (as mm/dd/yyy): ");
  8.         String date = scanner.next();
  9.        
  10.         String[] temp = date.split("/");
  11.         int month = Integer.parseInt(temp[0]);
  12.         int day = Integer.parseInt(temp[1]);
  13.         int year = Integer.parseInt(temp[2]);
  14.         boolean leapYear;
  15.        
  16.         if (year < 1 || year > 9999)
  17.             System.out.println("invalid year value");
  18.         else if (month < 1 || month > 12)
  19.             System.out.println("invalid month value");
  20.         else
  21.         {
  22.             if(day < 1)
  23.                 System.out.println("invalid day input");
  24.             else if(month == 2 && leapYear && day > 28)
  25.                 System.out.println("invalid day input");
  26.             else if(month == 2 && leapYear && day > 29)
  27.                 System.out.println("invalid day input");
  28.             else if((month == 4 || month ==6 || month ==9 || month== 11) && day > 30)
  29.                 System.out.println("invalid day");
  30.             else if((month ==1 || month ==3 || month ==5 || month == 7 || month ==8 ||month ==10 || month ==12) && day >31)
  31.                 System.out.println("date + is a valid date.");
  32.         }
  33.     }
  34. }
  35.  
  36.  
  37.  
  38. //Command Prompt
  39. //C:\COMP170\Java Programs>javac ValidDate.java
  40. //ValidDate.java:24: error: variable leapYear might not have been initialized
  41.                         else if(month == 2 && leapYear && day > 28)
  42.                                               ^
  43. //ValidDate.java:26: error: variable leapYear might not have been initialized
  44.                         else if(month == 2 && leapYear && day > 29)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top