Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

My code is not running correctly and I am not sure why. Usually anything I enter

ID: 3798067 • Letter: M

Question

My code is not running correctly and I am not sure why. Usually anything I enter reads as the current date.

The question :

Use the Web to learn how to use the LocalDate Boolean methods isBefore(), isAfter(), and equals(). Use your knowledge to write a program that prompts a user for a month, day, and year, and then displays a message specifying whether the entered day is in the past, the current date, or in the future. Save the file as PastPresentFuture2.java

My code:

import java.util.Scanner;
import java.time.LocalDate;

public class PastPresentFuture2
{
   static final int month = 0;
   static final int day = 0;
   static final int year = 0;
  
   public static void main(String[] args)
   {
       Scanner enter = new Scanner(System.in);
      
       LocalDate currentDate = LocalDate.now();
      
       System.out.println("Enter year");
       int year = enter.nextInt();
       System.out.println("Enter month");
       int month = enter.nextInt();
       System.out.println("Enter day");
       int day = enter.nextInt();
      
       LocalDate inputDate = LocalDate.of(year,month,day);
      
       if (inputDate.isBefore(currentDate))
       {
           System.out.println("This date is the current date.");
       }
  
       else if (inputDate.isAfter(currentDate))
       {
           System.out.println("This date is in the future.");
       }
  
       else if (inputDate.isEqual(currentDate))
       {
           System.out.println("This is the current date.");
       }
  
  
   }

}

Explanation / Answer

There is only one small mistake in your given code.

in this comparison :

if (inputDate.isBefore(currentDate))
       {
           System.out.println("This date is the current date.");
       }

you must print that date is before current date instead of "this date is current date".

corrected code is here :

import java.util.Scanner;
import java.time.LocalDate;

public class PastPresentFuture2
{
   static final int month = 0;
   static final int day = 0;
   static final int year = 0;
  
   public static void main(String[] args)
   {
       Scanner enter = new Scanner(System.in);
      
       LocalDate currentDate = LocalDate.now();
      
       System.out.println("Enter year");
       int year = enter.nextInt();
       System.out.println("Enter month");
       int month = enter.nextInt();
       System.out.println("Enter day");
       int day = enter.nextInt();
      
       LocalDate inputDate = LocalDate.of(year,month,day);
      
       if (inputDate.isBefore(currentDate))
       {
           System.out.println("This date is BEFORE the current date.");
       }
  
       else if (inputDate.isAfter(currentDate))
       {
           System.out.println("This date is in the future.");
       }
  
       else if (inputDate.isEqual(currentDate))
       {
           System.out.println("This is the current date.");
       }
  
  
   }

}

If there is any other doubt regarding solution , you can ask in comment section.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote