Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6999589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:33:40+00:00 2026-05-27T20:33:40+00:00

So, I recently read this post and I want to do effectively the same

  • 0

So, I recently read this post and I want to do effectively the same thing.

I didn’t really care about “ugliness”, etc. so I implemented one of the methods like so:

public enum Day {

    Monday(1), 
    Tuesday(2), 
    Wednesday(3), 
    Thursday(4), 
    Friday(5), 
    Saturday(6), 
    Sunday(7);

    public final int id;

    Day(int id) {
        this.id = id;
    }

    public static Day getByID(int id) {
        Day d = null;
        for (Day dTemp : Day.values())
        {
            if (id == dTemp)
            {
                d = dTemp;
                break;
            }
        }

        return d;
    }

    public Day getNext() {
        return values()[(ordinal()+1)%values().length];
    }

    public Day getPrev() {
        return values()[(ordinal()-1)%values().length];
    }
}

But, the problem with it is in the if statement when I do:

if (id == dTemp)

It says that they are incompatible types. What should I do to fix it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:33:41+00:00Added an answer on May 27, 2026 at 8:33 pm

    Use if ( id == dTemp.id ). Enums are classes and enum values are objects not ints and thus can’t be cast (neither explicitly nor implicitly) to an int.

    Alternatively, note that enums have an ordinal, i.e. an id of their position. In your example, Monday would have the ordinal 0 and Sunday would have the ordinal 6. You access the ordinal by calling the ordinal() method.

    Thus if id is one-based you could do the following instead:

    public static Day getByID(int id) {   
      //check id is between 1 and 7, I'll leave that for you
      return Day.values()[id - 1];
    }
    

    Note that you might want to cache Day.values() in a private static variable and access that cached array then.

    Btw, where’s the string you mention in your question?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently read this post: Floating point vs integer calculations on modern hardware and
Recently, I read a post on Stack Overflow about finding integers that are perfect
This may sound silly but I didn't want to hijack a question. I recently
I recently read a blog post about the Reddit hotness formula . The formula
I recently read this question which had a solution about labeling loops in Java.
I recently read this thread on MSDN . So I was thinking of using
Ever wonder what wikipedia's database schema looks like? I recently read this thread from
Recently I read a book( CleanCode ) in this book, FUNCTION SHOULD DO ONE
I've read a great deal of discussion recently (both on this site and elsewhere)
I recently read a nice post on using StringIO in Ruby. What the author

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.