• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Code to Find Leap Year Error (Code, Error, and Attempts to Fix Included)

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Objective of Code: I need a write a code that will allow for a user to insert a year, and the code will tell them if it is a leap year by dividing it by 4. If the year is a leap year, the code will output the text "[YEAR] is a leap year." If not, it will produce "[YEAR] is NOT a leap year."

Error with the Code (as it is included below): Whenever I run the code, I do get the option to enter the year I want to evaluate. However, once I enter it, nothing happens. I'm not receiving any specific error message. I thought this meant that I needed to change my code in some way to include some sort of message to output a statement like those included above ("[YEAR] is/is NOT a leap year."), but my attempts (included after my code) to fix it haven't worked.

Code (As Is):

The following code is in the main of one class (Main.java).



The following code is in the main of another class (LeapYear.java).



Attempt to fix 1: I added the statement-



To my main in Main.java after-

         

Because there was a similar statement included in another batch of code I'm worked on a few weeks ago. However, this change resulted in this error:



I'm not quite sure what this error means (I'm pretty new to Java), so I just tried something else.

Attempt to fix 2: I deleted the code I added in Attempt to fix 1 and tried to make my return statements-



Into statements like-

       

and-

       

But I got the error that I was missing a return statement. I tried adding return to the beginning of the new out.println statements above, but got this error-



In Conclusion

How can I make my code output the appropriate "[YEAR] is/is NOT a leap year." statements? Thank you!

(I tried to be thorough in my explanation of what I was trying to do/what I tried in order to fix my error, but if I missed something please let me know!)
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Your method isLeapYear() is static to the LeapYear class so you have to call it in a static manner using the class name:
It returns a String so you have to output that when the method returns.
 
Jeremiah Waters
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Carey!

I made your suggested adjustment, but now I'm receiving this error. What does this mean?

 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've posted so many different changes. This is what you had at one point:
In order to help you you need to post the current code in its entirety and any error messages in their entirety.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Weelcome to the Ranch

Jeremiah Waters wrote: . . .

You seem to have lots of static imports, but are you actually using them?

The following code is in the main of another class (LeapYear.java).

Please don't go thinking there is anything important about the main method. It is like the Union Flag at the Grand Prix at Silverstone. The Union Flag isn't really important: the cars are the important part. It is there for starting the application but there is nothing important about the main method. More information here.



[code=java:fistline[8]]public static String isLeapYear( int year )
//The String above was originally Boolean, but I changed it because Boolean caused a whole other sling of errors.[/code]

Don't change your code to correct the error messages. Use the error messages to find the error in your code and change the code to correct the errors. A method with a name starting is should usually return a boolean (not a Boolean). So make your method return a boolean, and run your code until you can pass years and get the results true or false. That shouldn't take long. Note what this old style guide says about boolean returns.

Attempt to fix 1: . . .

Program to the design of your application, not to get rid of the compiler errors. You have two options. You can design a class representing a year, in which case it would have a year number as a field and a method to show whether it is a leap year. That is one option. You would not have a method taking a year number to show whether it is a leap year. That means you have to create a standard Java® class and make objects from it.
The alternative, which is less object‑oriented, would be not to have any fields and make all method static as you are already trying. You don't need any objects of the LeapYear class.
 
Jeremiah Waters
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Carey! Sorry for the delay!

Here is my current code:



and

 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Carey's suggesting you go back to your original posted code.
 
Jeremiah Waters
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Dave and Carey! It's working now!
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeremiah Waters wrote:Thank you Dave and Carey! It's working now!


I'm glad you got it to work. Very good.
It would be appreciated if you posted your final working code here so that others may learn from it.
 
Jeremiah Waters
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

For anyone who's experiencing a similar issue, here's my final code!

This code is in the main of class Main.java:



This code is in the main of class LeapYear.java:



Best of luck!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic