0

Can anyone please answer the below questions?

  1. Can I use logical operator over enum values? For example:

    enum Days { MONDAY(1), TUESDAY(2), WEDNESDAY(3), THURSDAY(4), FRIDAY(5), SATURDAY(6), SUNDAY(7) } 

    now can I use if(Days.SUNDAY > Days.MONDAY) ....?

  2. Is there any process to specify a range of values for enum? For example, I want to specify the valid numbers for an exam should be between 1 - 100. So if I want to declare an enum, then it would be like this -> enum Validno{ONE(1),TWO(2),....,HUNDRED(100)} Now is there any option to declare an enum containing the each values and specify them like a range instead of declaring each one seperately?

    • Thanks in advance.
9
  • 5
    Possible and possible. Commented Mar 16, 2014 at 15:15
  • 8
    For #1, couldn't you just have tried it? Commented Mar 16, 2014 at 15:16
  • For #2, it's not clear what you want. Are you asking whether there's a way to generate the enum ONE(1), TWO(2), ... HUNDRED(100) without actually typing it all in to your source code? Commented Mar 16, 2014 at 15:17
  • 1
    With not just use an int for #2? It's not the best use of enums. Commented Mar 16, 2014 at 15:19
  • You better use enums for a small set. 100 values I would say it's too big to use as an enum. Commented Mar 16, 2014 at 15:24

3 Answers 3

1

For #1, you can use ordinal method as below

enum Day { MON, TUE, WED, THU, FRI, SAT, SUN } public class EnumExample { public static void main(String[] args) { System.out.println(Day.MON.ordinal() < Day.TUE.ordinal()); // Prints true System.out.println(Day.MON.ordinal() > Day.TUE.ordinal()); // Prints false } } 

for #2: why do you want to use enums? You can simple comparison. Like for eg:

if(marks > 0 && marks < 100) 
Sign up to request clarification or add additional context in comments.

1 Comment

You can also use Day.MON.compareTo(Day.TUE) > 0. Enums automatically implement Comparable.
1

For #1, yes it's possible using compareTo method.

You could do something like this:

enum Days { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } public static void main(String[] args) { System.out.println(Days.Monday.compareTo(Days.Sunday)); // Returns negative value System.out.println(Days.Thursday.compareTo(Days.Tuesday)); // Returns positive value System.out.println(Days.Tuesday.compareTo(Days.Tuesday)); // Returns zero } 

For #2, I don't think there's a way to declare enums programatically. You will have to list down ONE to HUNDRED, if you want to use enum.

Instead, why don't you just declare a function that checks if a given score is valid or not? It could be something like this:

public boolean isValid(int score){ return score>=0 && score<=100; } 

Comments

1

As an alternative for your second problem, you can simply use a regular class:

public final class Score implements Comparable<Score> { private final int value; public Score(int value) { if (value < 0 || value > 100) { throw new IllegalArgumentException("score value must be between 0 and 100"); } this.value = value; } public int getValue() { return value; } // equals, hashCode and compareTo omitted, but should be there } 

This gives you the type safety of an enum, without the need to declare every of the 101 values independantly. No one will ever be able to create a score with an incorrect value.

1 Comment

Thanks for ur suggestion ... actually I want to assign a pririty for the objects of a certain class. Now the priority should be in between 1-100 ... that's why I am thinking of using enum... but while I am writing code, I have to type ONE to HUNDRED ... that's the problem ...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.