-1

I have run this code and looked online for solutions. How can I fix this error? I saved PastPresentFuture.java and it still won't run.

YourName.java:7: error: class PastPresentFuture is public, should be declared in a file named PastPresentFuture.java
public class PastPresentFuture
       ^
1 error

Code:

//Name
//Date
//Chapter and problem #
//Application prompts user to enter month, day, year 
import java.util.*;
import java.time.LocalDate;
public class PastPresentFuture
{
   public static void main (String[] args) 
   {
      LocalDate today = LocalDate.now(); //Creates the local class of today
      int mo, da, yr;
      int todayMo, todayDa, todayYr;
      Scanner input = new Scanner(System.in);
      todayMo = today.getMonthValue();
      todayDa = today.getDayOfMonth();
      todayYr = today.getYear();

//Input month, day and year enter text for month, day and year
      System.out.print("May");
      mo = input.nextInt();
      System.out.print("18");
      da = input.nextInt();
      System.out.print("2018");
      yr = input.nextInt();

//Reference if and else statement example on p. 274 
//Display text statements shown as (1, 2, 3, 4) on p. 295 for this problem
      if(yr != todayYr)
         System.out.println(yr + todayYr);
      else
         if(mo < todayMo)
            System.out.println(mo + todayMo);
         else
            if(mo > todayMo)
               System.out.println(mo +todayMo);
            else
               System.out.println(mo +todayDa);
   }
}
1

1 Answer 1

1

Your class is called PastPresentFuture and I'm guessing by the error message that your file is not PastPresentFuture.java. you need to change it to be that so it matches the class name.

Edit: This only applies to public classes.

Sign up to request clarification or add additional context in comments.

2 Comments

This is only required for public classes, but it is good practice to name all files for the top level class it declares, public or otherwise (e.g. to help you find the source for a class).
@AndyTurner Agreed, I was just pointing out the fact that you could if you wanted to change the names for non-public classes.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.