Results 1 to 14 of 14
  1. #1
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Why are my enum types not being recognized by my methods?

    Attempting to write a custom comparator for sorting songs, but I keep getting errors saying that the types cannot be resolved.
    Am I missing something?

    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    import java.util.*;
    
    public class SongCollection {
    	ArrayList<Song> songs2;
    	ArrayList<Song> itemsToRemove = new ArrayList<Song>(); // second collection
    															// for items to
    															// remove
    
    	public SongCollection(ArrayList<Song> songs) { // constructor for
    													// SongCollection
    		this.songs2 = songs;
    	}
    	enum PersonComparator implements Comparator<Song> { //custom comparator 
    	    YEAR_SORT {
    	        public int compare(Song o1, Song o2) {
    	            return Integer.valueOf(o1.getYear()).compareTo(o2.getYear());
    	        }},
    	        
    	    RANK_SORT {
    		    public int compare(Song o1, Song o2) {
    		        return Integer.valueOf(o1.getRank()).compareTo(o2.getRank());
    		    }},
    	    ARTIST_SORT {
    	        public int compare(Song o1, Song o2) {
    	            return o1.getArtist().compareTo(o2.getArtist());
    	        }},
    	    TITLE_SORT {
    		    public int compare(Song o1, Song o2) {
    		        return o1.getTitle().compareTo(o2.getTitle());
    		    }};
    	    public static Comparator<Song> descending(final Comparator<Song> other) {
    	        return new Comparator<Song>() {
    	            public int compare(Song o1, Song o2) {
    	                return -1 * other.compare(o1, o2);
    	            }
    	        };
    	    }
    
    	    public static Comparator<Song> getComparator(final SongComparator... multipleOptions) {
    	        return new Comparator<Song>() {
    	            public int compare(Song o1, Song o2) {
    	                for (SongComparator option : multipleOptions) {
    	                    int result = option.compare(o1, o2);
    	                    if (result != 0) {
    	                        return result;
    	                    }
    	                }
    	                return 0;
    	            }
    	        };
    	    }
    	}
    	public void filterYear(Range r) {
    		int n = 0;
    		if (n == 0) {
    			System.out.println("Program is processing.");
    			n++;
    			for (Song song1 : songs2) {
    				if (song1.year > (r.getMax()) || (song1.year) < (r.getMin())) {
    					itemsToRemove.add(song1);
    				}
    			}
    			songs2.removeAll(itemsToRemove);
    			itemsToRemove.clear();
    		}
    	}
    
    	public void filterRank(Range r) {
    		int n = 0;
    		if (n == 0) {
    			System.out.println("Program is processing.");
    			n++;
    			for (Song song1 : songs2) {
    				if (song1.rank > (r.getMax()) || (song1.rank) < (r.getMin())) {
    					itemsToRemove.add(song1);
    				}
    			}
    			songs2.removeAll(itemsToRemove);
    			itemsToRemove.clear();
    		}
    	}
    
    	public void filterArtist(String s) {
    		int n = 0;
    		if (n == 0) {
    			System.out.println("Program is processing.");
    			n++;
    			for (Song song1 : songs2) {
    				if ((!(((song1.getArtist()).contains(s))))) {
    					itemsToRemove.add(song1);
    				}
    			}
    			songs2.removeAll(itemsToRemove);
    			itemsToRemove.clear();
    		}
    	}
    
    	public void filterTitle(String s) {
    		int n = 0;
    		if (n == 0) {
    			System.out.println("Program is processing.");
    			n++;
    			for (Song song1 : songs2) {
    				if ((!(((song1.getTitle()).contains(s))))) {
    					itemsToRemove.add(song1);
    				}
    			}
    			songs2.removeAll(itemsToRemove);
    			itemsToRemove.clear();
    		}
    	}
    
    	public void sortTitle(SongComparator.Order sortingBy) {
    		Collections.sort(songs2, getComparator(TITLE_SORT)); //TITLE_SORT cannot be resolved to a variable
    	}
    	public void sortRank() {
    		Collections.sort(songs2, getComparator(RANK_SORT)); //RANK_SORT cannot be resolved to a variable
    	}
    
    	public void sortArtist() {
    		Collections.sort(songs2, getComparator(ARTIST_SORT)); //ARTIST_SORT cannot be resolved to a variable
    	}
    
    	public void sortYear() {
    		Collections.sort(songs2, getComparator(YEAR_SORT)); //YEAR_SORT cannot be resolved to a variable
    	}
    
    	public String toString() {
    		String result = "";
    		for (int i = 0; i < songs2.size(); i++) {
    			result += " " + songs2.get(i);
    		}
    
    		return result;
    
    	}
    }

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: Why are my enum types not being recognized by my methods?

    Very unique code that, I haven't seen an enum used like that. But doesn't the code you posted contain a copy/paste mistake? You refer to a SongComparator yet the enum you have in your code is called 'PersonComparator'.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: Why are my enum types not being recognized by my methods?

    Oh, oops. Fixed that.
    Also took out the parameter in sortTitle(), which was left over from a previous attempt.
    Unfortunately the error still persists.

  4. #4
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: Why are my enum types not being recognized by my methods?

    Never mind, it's still not working. D:
    Last edited by Propinquity; 03-17-2014 at 11:35 AM.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: Why are my enum types not being recognized by my methods?

    I don't understand the need for your 'getComparator()' static function; you define the comparators in an enum so they're effectively singleton instances. You can just get one by referring to them like this:

    Java Code:
    Collections.sort(songs2, SongComparator.TITLE_SORT);
    That getComparator() code doesn't seem logical to me anyway, this bit:

    Java Code:
    if (result != 0) {
      return result;
    }
    Basically says 'if the two are NOT equal, return it'.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: Why are my enum types not being recognized by my methods?

    I changed my methods to this:
    Java Code:
    	public void sortTitle() {
    		Collections.sort(songs2, getComparator(SongComparator.TITLE_SORT)); //TITLE_SORT cannot be resolved to a variable
    	}
    	public void sortRank() {
    		Collections.sort(songs2, getComparator(SongComparator.RANK_SORT)); //RANK_SORT cannot be resolved to a variable
    	}
    
    	public void sortArtist() {
    		Collections.sort(songs2, getComparator(SongComparator.ARTIST_SORT)); //ARTIST_SORT cannot be resolved to a variable
    	}
    
    	public void sortYear() {
    		Collections.sort(songs2, getComparator(SongComparator.YEAR_SORT)); //YEAR_SORT cannot be resolved to a variable
    	}
    And now it's saying that the method getComparator(SongCollection.SongComparator) is undefined for the type SongCollection.
    But the method is right there!
    Java Code:
     public Comparator<Song> getComparator(final SongCollection.SongComparator option) {
    	        return new Comparator<Song>() {
    	            public int compare(Song o1, Song o2) {	          
    	                    int result = option.compare(o1, o2);
    	                    if (result != 0) {
    	                        return result;
    	                    }
    	                
    	                return 0;
    	            	};
    	    		};
    	        };
    	    }

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: Why are my enum types not being recognized by my methods?

    You are ignoring effectively 75% of my previous post.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: Why are my enum types not being recognized by my methods?

    Is the same problem" How would I accomplish this?
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: Why are my enum types not being recognized by my methods?

    You know, in a different post, you asked how to reduce the size of your code (and you didn't respond to my reply). In this situation, you still need code to call the various sort methods. But why do that? All you need is to call one sort method and pass the appropriate enum to the method. In other words, why do four tests to call one of four methods when you can do four tests to call one method and get the same result?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: Why are my enum types not being recognized by my methods?

    Quote Originally Posted by jim829 View Post
    But why do that? All you need is to call one sort method and pass the appropriate enum to the method. In other words, why do four tests to call one of four methods when you can do four tests to call one method and get the same result?
    (basically what I demonstrate in post #5 right?)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: Why are my enum types not being recognized by my methods?

    Oops. Totally missed that. Yep.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: Why are my enum types not being recognized by my methods?

    Judges must wait until someone brings an issue before them before they can rule. Well, I am going to break that rule. You do not have to follow my suggestion but it may be a learning experience. Here is one of your filters.

    Java Code:
        public void filterRank(Range r) {
            int n = 0;
            if (n == 0) {
                System.out.println("Program is processing.");
                n++;
                for (Song song1 : songs2) {
                    if (song1.rank > (r.getMax()) || (song1.rank) < (r.getMin())) {
                        itemsToRemove.add(song1);
                    }
                }
                songs2.removeAll(itemsToRemove);
                itemsToRemove.clear();
            }
        }
    My understanding is that you want to return only the songs whose rank is between max and min inclusive. So you reverse your condition test and find records that don't meet that criteria. Then you remove those records from your list which also destroys your original list. Here are my questions:

    1. Why not just use the normal criteria and add those records to a new ArrayList. Then just replace the main array list with that?

    2. Why destroy anything? Why not simply create a new instance of SongCollection and as you find records that match the filter, add them to the new SongCollection. Then you can just return that SongCollection. This last recommendation allows you to further filter your new collection and it preserves the original. And you don't need a second ArrayList.

    Now if I misinterpreted anything or made a mistake please let me know (as though that won't happen in this forum) ;)

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  13. #13
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: Why are my enum types not being recognized by my methods?

    One problem at a time!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: Why are my enum types not being recognized by my methods?

    Right. It was a moment of weakness on my part. But some questions may never be asked.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. public static enum vs enum class
    By Dipke in forum New To Java
    Replies: 3
    Last Post: 08-30-2011, 11:45 AM
  2. generic and different enum types
    By vojtab in forum New To Java
    Replies: 0
    Last Post: 11-08-2010, 06:11 PM
  3. Setting values from One Enum type to another enum type.
    By reach2sudhakar in forum New To Java
    Replies: 3
    Last Post: 09-23-2010, 07:02 PM
  4. Replies: 1
    Last Post: 10-13-2009, 08:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •