Solved

Java Collections sort by name - syntax error

Posted on 2012-03-15
4
266 Views
Last Modified: 2012-03-15
Hi experts,

I have a problem using List, Collections.sort() <== got a syntax error

I have a List of data like below:

         SchoolContactInfo schoolContactInfo [] = {
                  new ArtStudent("Paul", "987654561"),
                  new ArtStudent("Avie", "987654561"),
                  new MathsStudent("John","98769832")};

         List<SchoolContactInfo > mylist = new ArrayList<SchoolContactInfo >();
         mylist.add(schoolContactInfo [0]);
         mylist.add(schoolContactInfo [1]);
         mylist.add(schoolContactInfo [2]);

                    Collections.sort(mylist);

What I want the output sorted by name using Collections sort but i got sysntax saying

SchoolContactInfo is not valid substitute of bounded parameters

My other class like below:

public abstract class SchoolContactInfo {
      private String name;
      private String phone;

      // Constructor
      public ContactInfo(String Name, String Phone) {
            name = Name;
            phone = Phone;
      }

      public String getName() {
            return name;
      }

      public void setName(String name) {
            this.name = name;
      }

      public String getPhone() {
            return phone;
      }

      public void setPhone(String phone) {
            this.phone = phone;
      }
}

public class ArtStudent extends SchoolContactInfo {
      // constructor
      public FriendA(String name, String phone) {
            super(name, phone);
      }
}

public class MathsStudent extends SchoolContactInfo {
      // constructor
      public FriendA(String name, String phone) {
            super(name, phone);
      }
}

===
please correct my syntax. notes i want the output of student sorted by the name
0
Comment
Question by:unistudent
  • 3
4 Comments
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
You class
SchoolContactInfo

should implement Comparabale interface
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
Comment Utility
this compiles without any error:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class School {

    public static void main(String[] args) {

          SchoolContactInfo schoolContactInfo [] = {
                  new ArtStudent("Paul", "987654561"),
                  new ArtStudent("Avie", "987654561"),
                  new MathsStudent("John","98769832")};

         ArrayList<SchoolContactInfo > mylist = new ArrayList<SchoolContactInfo >();
         mylist.add(schoolContactInfo [0]);
         mylist.add(schoolContactInfo [1]);
         mylist.add(schoolContactInfo [2]);

                    Collections.sort(mylist);

        
    }


}


  abstract class SchoolContactInfo implements Comparable <SchoolContactInfo>{
      private String name;
      private String phone;

      // Constructor
      public SchoolContactInfo(String Name, String Phone) {
            name = Name;
            phone = Phone;
      }

      public String getName() {
            return name;
      }

      public void setName(String name) {
            this.name = name;
      }

      public String getPhone() {
            return phone;
      }

      public void setPhone(String phone) {
            this.phone = phone;
      }
      
     public  int compareTo(SchoolContactInfo s){
         
         return s.getName().compareTo(name);
          
      }
}

 class ArtStudent extends SchoolContactInfo {
      // constructor
      public ArtStudent(String name, String phone) {
            super(name, phone);
      }
}

 class MathsStudent extends SchoolContactInfo {
      // constructor
      public MathsStudent(String name, String phone) {
            super(name, phone);
      }
}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
You also hneed to keep in mind that if you define constructor - it should have the same name as the class:

public class ArtStudent extends SchoolContactInfo {
      // constructor
      public FriendA(String name, String phone) {
            super(name, phone);
      }
}

Open in new window


this acuses erroer as you "contructor" has name FriendA ()
different form the name of the class ArtStudent - this causes compilatuion error - i corrected this in the code above
0
 

Author Closing Comment

by:unistudent
Comment Utility
so clear. excellent. i am glad he helps me.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now