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: Help needed with enum

  1. #1
    Junior Member
    Join Date
    Nov 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help needed with enum

    Hi there!

    First, I'm sorry if my post doesn't belong here, I registered today so I'm new here. I'm also french, so english is not my native language - sorry if I'm not precise and if you don't understand my message.

    I'm trying to create an enumeration called Origin with 3 parameters : a String (name), a list of Competence (which is a class that I created myself), and a list of Aspect (which I also created myself). I tried to create objects into this enumeration, but whatever I tried, I didn't manage to create one Origin. Here are some parts from my code, if you understood my explanation and have an element that might help me, thanks in advance!

    public enum Competence {
      Admin("Admin"),
      Medic("Medic"),
      Worker("Worker");
     
      private String name;
     
      Competence(String name) {
        this.name = name;
      }
    }
     
    public class Aspect {
      private String name;
     
      public Aspect(String name) {
        this.name = name;
      }
    }
     
    public enum Origin {
      Batra("Batra", "HERE IS THE Competence[]", "HERE IS THE Aspect[]");
     
      private String name;
      private Competence[] competences;
      private Aspect[] aspects;
     
      Origin(String name, Competence[] competences, Aspect[] aspects) {
        this.name = name;
        this.competences = competences;
        this.aspects = aspects;
      }
    }

    Once again, thank you in advance if you have an element that could help me progress in my personal project!

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

    Default Re: Help needed with enum

    There is an example for enum in the tutorial:
    https://docs.oracle.com/javase/tutor...vaOO/enum.html

    If you get error messages you need help with, copy the full text and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2024
    Posts
    75
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Help needed with enum

    It seems like you're on the right track with your code. However, you're encountering issues because you're passing strings where arrays of `Competence` and `Aspect` objects are expected when defining your `Origin` enum constants. Let's correct that.

    You need to pass arrays of `Competence` and `Aspect` objects instead of strings. Here's how you can do it:

    ```java
    public enum Origin {
    Batra("Batra", new Competence[]{Competence.Admin}, new Aspect[]{new Aspect("ExampleAspect")});

    private String name;
    private Competence[] competences;
    private Aspect[] aspects;

    Origin(String name, Competence[] competences, Aspect[] aspects) {
    this.name = name;
    this.competences = competences;
    this.aspects = aspects;
    }
    }
    ```

    In this corrected version, `Competence.Admin` represents an instance of the `Competence` enum, and `new Aspect("ExampleAspect")` represents an instance of the `Aspect` class. Make sure to adjust the parameters accordingly based on your actual data.

    With these adjustments, you should be on the right path to successfully create instances of the `Origin` enum and progress with your personal project. If you encounter any further challenges or need help with your Java assignment, feel free to ask for help from various programming communities, assignment help platforms like ProgrammingHomworkHelp.com or online resources available for learning and troubleshooting, as there are many valuable resources out there to support your learning journey.

Similar Threads

  1. Enum Values Each Having Their Own Enum Values
    By corosus in forum Object Oriented Programming
    Replies: 1
    Last Post: July 5th, 2013, 07:04 AM
  2. Grails Developer Needed - URGENTLY NEEDED *WORK FROM HOME*
    By IngeniumR in forum Paid Java Projects
    Replies: 0
    Last Post: February 5th, 2013, 07:19 AM
  3. enum
    By anis.laghaei in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 23rd, 2012, 03:19 PM
  4. [SOLVED] Alternative to Enum
    By Doctor X in forum Java Theory & Questions
    Replies: 2
    Last Post: October 6th, 2012, 03:49 PM
  5. [SOLVED] Enum types, how does he know %s value?
    By beer-in-box in forum Java Theory & Questions
    Replies: 11
    Last Post: September 17th, 2011, 12:47 PM