Results 1 to 9 of 9
  1. #1
    utachan is offline Member
    Join Date
    Dec 2013
    Posts
    5
    Rep Power
    0

    Default probem with java generic

    I tried creating a TreeSet, each member of it will have 2 fields 1 is float and the other is either int or string depands what the user wants.
    I tried creating it and I am not sure how to do it but it doesn;t work :(
    the thing is I am only allowed to use 1 string (ignoring the tostring's string and the main;s String args)
    and 5 Integers in the program.
    for some reason it actually complains about comparable....I have no idea what it wants could someone please help me with it cause I am lost.
    (please igniore the tostring I have not thought about it yet)

    this is my code:

    Java Code:
    package ex2;
    import java.util.Scanner;
    import java.util.Set;
    import java.util.TreeSet;
    
    public class Program 
    {
    	static Scanner scan=new Scanner(System.in);
    	public static void main(String[] args)
    	{				
    		System.out.println("Plese enter how many courses");
    		int numOfCourses=scan.nextInt();
    		
    		System.out.println("Please press 1 to enter the course by name, 2 by id:");				
    		int type=scan.nextInt();			
    			
    		Set<Course<?>> set=new TreeSet<Course<?>>();							
    		set=CreateList(set,numOfCourses,type);		
    		
    	}
    
    	public static <E> Set<Course<?>> CreateList(Set<Course<?>> set,int num,int type)
    	{		
    		if(type==1)
    		
    			for(int i=0;i<num;i++)
    			{
    				System.out.println("Please enter name");
    				scan.next();
    				String name=scan.nextLine();
    				System.out.println("Please enter avg");
    				float avg=scan.nextFloat();
    				
    				set.add(new Course<String>(name,avg));						
    				System.out.println(set);
    			}
    		
    		else
    		{			
    			for(int i=0;i<num;i++)
    			{				
    				int id=scan.nextInt();
    				float avg=scan.nextFloat();
    				
    				set.add(new Course<Integer>(id,avg));							
    			}
    		}
    		
    		return set;
    	}
    	
    	static public class Course<E>
    	{
    		private E idOrName;
    		private float avg;
    		
    		public Course(E idOrName,float avg)
    		{
    			this.idOrName=idOrName;
    			this.avg=avg;
    		}
    		
    		public  String toString()
    		{			
    			return String.format(" %15s", this.idOrName);			
    		}
    		
    		
    	}	
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default Re: probem with java generic

    You say it doesn't work and it complains, but what doesn't work and what is the exact error you're seeing?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    utachan is offline Member
    Join Date
    Dec 2013
    Posts
    5
    Rep Power
    0

    Default Re: probem with java generic

    Sorry I forgot to add it X_X
    I tried to enter values and this is the error I got:
    Exception in thread "main" java.lang.ClassCastException: ex2.Program$Course cannot be cast to java.lang.Comparable
    at java.util.TreeMap.compare(Unknown Source)
    at java.util.TreeMap.put(Unknown Source)
    at java.util.TreeSet.add(Unknown Source)
    at ex2.Program.CreateList(Program.java:37)
    at ex2.Program.main(Program.java:18)

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default Re: probem with java generic

    Well, the error code pretty much explains it: you're using a TreeSet, which require Comparable objects. You're inserting instances of Course, which is not Comparable.

    For more info, check out the API for TreeSet: TreeSet (Java Platform SE 7 )

    Especially this part: "All elements inserted into the set must implement the Comparable interface."
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    utachan is offline Member
    Join Date
    Dec 2013
    Posts
    5
    Rep Power
    0

    Default Re: probem with java generic

    Thanks :) I did try to play with it but what I missed was actually I had to override the compareTo method I tried adding " implements Comparable<E>" and I got an error and I thought E shouldn;t go there I must b doing something wrong.

    But now I have a problem, any idea how I can add compareto method while E idOrName could b either String or int?
    Is there a way to tell whether E is int or string?

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default Re: probem with java generic

    Recommended reading: Object Ordering (The Java™ Tutorials > Collections > Interfaces)

    Both Integer and String implement Comparable.

    If you know E is always Comparable, you don't have to check the type.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    utachan is offline Member
    Join Date
    Dec 2013
    Posts
    5
    Rep Power
    0

    Default Re: probem with java generic

    I get that , the thing I am troubled with is by letting Course implement comparable means I must override the compareto method.
    I tried :
    Java Code:
    public int compareTo(E e) {
    			if (this.idOrName==e);
    			return 0;
    with teh input:

    Java Code:
    Course<Integer> c1=new Course<Integer>(12,100);
    	Course<Integer> c2=new Course<Integer>(11,100);
    and got 0

    I tried e.Compareto but it brings nothing
    I tried Instead of
    Java Code:
    compareTo(E e)
    Doing:
    Java Code:
    compareTo(Course<E> c)
    No idea.

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default Re: probem with java generic

    Well you've got a couple strange things going on. First of all, you have an if statement that ends with a semicolon, so the line (return 0) right after it is going to happen regardless of that if statement.

    Secondly, you have to take a step back and look at it this way: you have an object (Course<E>) that contains another value (a String or an Integer). You want to make Course<E> instances comparable, so you implement Comparable<Course<E>>. For a Course<E> to be Comparable, it has to implement the compareTo(Course<E> other)(...) method. Once you have that, you have to define how two instance of Course<E> should be compared. You say you want to compare the objects they contain, which are of type E. Since E extends Comparable, you know that E has a compareTo(E other) method.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    utachan is offline Member
    Join Date
    Dec 2013
    Posts
    5
    Rep Power
    0

    Default Re: probem with java generic

    Kevin I thank you very much for the help.
    I implemented it for <E> so no wonder it couldn;t work (Instead of implementing for Course<E> which allows me easy usage of compare to of E )
    I got it working now.
    Again thanks.

Similar Threads

  1. Probem In Netbeans
    By amitabh in forum NetBeans
    Replies: 2
    Last Post: 09-03-2010, 11:57 AM
  2. Compiling probem "cannot find symbol"
    By thegluups in forum New To Java
    Replies: 27
    Last Post: 01-18-2010, 08:53 PM

Posting Permissions

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