Results 1 to 3 of 3

Thread: Not getting proper result after using compareTo()

  1. #1
    Join Date
    Sep 2010
    Posts
    21

    Not getting proper result after using compareTo()

    Hello everybody, I am having an issue with the role beyond compareTo(). I create a Student class that implements the Comparable interface. The following is the sample of code :
    Code:
    class Student implements comparable {<Student> 
    private String name; 
    private int age; 
    // Constructor 
    // Setters and Getters 
    public int compareTo (Student E) { 
    return name.compareTo (E.get Name ());}// this method does work and throws compilation error
    but when
    public int compareTo (Student E) {
    return Age.compareTo (E.getAge it gives me an error ());}
    Thank you

  2. #2
    Join Date
    Jan 2009
    Posts
    140

    Re: Not getting proper result after using compareTo()

    According to me, Age is an int is a primitive type, it does not compareTo(). return to age - E.getAge(). Watch your way to code, the standards require that the class variables and methods begin with a lowercase letter. Many times there are some minute mistakes due to which you get compilation error. Otherwise compareTo is used to sort these types of objects in the lists more often. If you still getting problem, feel free to write.

  3. #3
    Join Date
    Jan 2009
    Posts
    143

    Re: Not getting proper result after using compareTo()

    You should do:
    Code:
    public int compareTo (Student e) {return (age-e.getAge ());}
    For method Collections.sort (), you just pass it a list of students like your class implements the Comparable interface:
    Code:
    Collections.sort (studentslist);
    Hope that you will get some hints from this. If not then try some R & D. till you succeed.

Similar Threads

  1. Replies: 4
    Last Post: 24-04-2012, 07:53 AM
  2. Windows 7 searching does not show proper result
    By Elazar in forum Operating Systems
    Replies: 4
    Last Post: 17-01-2011, 11:56 PM
  3. Click here to see F.E.(Sem.-II)(Rev.) Result
    By Users in forum Education Career and Job Discussions
    Replies: 7
    Last Post: 13-07-2009, 09:33 AM
  4. F.E. result - Sem I (2009)
    By BhavinK in forum Education Career and Job Discussions
    Replies: 1
    Last Post: 03-02-2009, 09:30 AM
  5. Check your MH CET result Here
    By Amol21 in forum Education Career and Job Discussions
    Replies: 3
    Last Post: 14-06-2008, 07:19 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,566,295.57990 seconds with 16 queries