Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How do I compare a variable's value with an enum's value?

  1. #1
    Junior Member
    Join Date
    Aug 2017
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How do I compare a variable's value with an enum's value?

    For example the context could be to see if a person can enter the club or not being underage or legal.

    In an enum class named Age:

    package test;
     
    public enum Age {
     
    	Underage(0), Legal(18)
     
    	int minAge;
    	Age(int minAge)
    	{
    		this.minAge = minAge;
    	}
    }

    If I have another class named Customer:

    package test;
     
    public class Customer 
    {
    	static Customer customer;
            static int age;
     
    	public Customer(int age)
    	{
    		this.age = age;
    	}

    Of course there would be input on their age. My question is how could I compare their age to the enum's value/minAge?

    e.g. If the customer was 14, I want to compare this to see if they are less than 18(value of Legal enum variable) in an if statement.

    I was thinking something like this:
    if(customer.age() >= Age.valueOf(minAge(Legal))

    Thanks in advance

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do I compare a variable's value with an enum's value?

    Try adding a boolean method to the enum to test the value.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    2
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How do I compare a variable's value with an enum's value?

    I think this:

    What you are showing here it should be something like:

    if(
    customer.age //it is a variable not a method so no '()'
    >= Age.valueOf("Legal")) // it is valueOf(String)

Similar Threads

  1. how do you compare words?
    By beginner123 in forum Java Theory & Questions
    Replies: 12
    Last Post: November 18th, 2012, 08:44 AM
  2. image compare
    By ana0429 in forum Member Introductions
    Replies: 1
    Last Post: March 21st, 2012, 03:03 AM
  3. Compare method
    By Evelina in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 16th, 2010, 02:07 PM
  4. Trying to somehow Compare Generics
    By Omega_ryan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2010, 12:58 PM