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 7 of 7

Thread: Simple enum question!

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Location
    sweden
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Simple enum question!

    Hi, im having some problems with adding an enum value to an object in an arraylist.

    I've highlighted the part that i can't get to work: rating.five (could not highlight the codesnip)
    mediaList.add(new Movie("title", 125, 2012, false, "Director", "path too hdd", rating.five, "HD", false, "Spanish", "Writer"));

    Enom code:
    public enum rating{one,two,three,four,five};
    private rating myRating;

    I tried importing Media.rating as well but without any luck, help is appreciated.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Simple enum question!

    Can you post an MCVE that demonstrates the problem you're talking about? What error are you getting?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Location
    sweden
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Simple enum question!

    Super:
    package thiistest;
     
    public abstract class one {
     
    	public enum number{one,two}
    	private number myNumber;
    	public one(number myNumber) {
    		super();
    		this.myNumber = myNumber;
    	}
    	public number getMyNumber() {
    		return myNumber;
    	}
    	public void setMyNumber(number myNumber) {
    		this.myNumber = myNumber;
    	}
    	@Override
    	public String toString() {
    		return "one [myNumber=" + myNumber + "]"+toStringSpec();
    	}
     
    	public abstract String toStringSpec();
    }
    Sub:
    package thiistest;
     
    public class two extends one {
     
    	private String name;
     
    	public two(number myNumber, String name) {
    		super(myNumber);
    		this.name = name;
    	}
     
    	public String getName() {
    		return name;
    	}
     
    	public void setName(String name) {
    		this.name = name;
    	}
     
    	public String toStringSpec() {
    		return "two [name=" + name + "]";
    	}
     
    }

    Test:
    package thiistest;
     
    import java.util.ArrayList;
     
    import thiistest.one.number;
     
    public class tersfzdsafds {
     
    	ArrayList<one> foo = new ArrayList<one>();
     
    	public void addTest() {
    		foo.add(new two(number.one,"name"));
    	}
    }

    It works when i do it now however i noticed that "package thiistest;" is included tho the path is not included in the project i'm working in, why is that?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Simple enum question!

    I don't really know what you're asking. Where isn't the package included?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Location
    sweden
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Simple enum question!

    In the project im working in i dont have any packages included, the classes lay under the (default package). and in the MVCE i did import thiistest.one.number; got included which i tried including in the other project as well (import Media.rating; ) but it does not work.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Simple enum question!

    If your actual program doesn't have packages, then your example shouldn't have them either. That's the whole point.

    You can't import classes or enums from the default package into another class, even if that other class is also in the default package. However, you can just use the class name to refer to it instead.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Mar 2014
    Location
    sweden
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Simple enum question!

    Anyhow i put it in a package now and it works, thanks for the help. Im not used to java or Eclipse and the only experience i have is with c++ so its a learning path for me.

Similar Threads

  1. Help with assignment about a board game (enum and array question)
    By Kranti1992 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 12th, 2012, 07:47 AM
  2. Help with simple enum problem
    By Prox in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 22nd, 2011, 03:19 PM
  3. Simple Question (hopefully)
    By KILL3RTACO in forum Java Theory & Questions
    Replies: 2
    Last Post: October 11th, 2011, 09:18 PM
  4. Simple I/O Question...well could be simple for you?
    By basketball8533 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 30th, 2011, 06:44 AM
  5. not so simple, simple swing question box
    By wolfgar in forum AWT / Java Swing
    Replies: 2
    Last Post: November 20th, 2009, 03:47 AM