Solved

Java Collections sort by name - syntax error

Posted on 2012-03-15
4
270 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
ID: 37727226
You class
SchoolContactInfo

should implement Comparabale interface
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 37727240
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
ID: 37727242
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
ID: 37727765
so clear. excellent. i am glad he helps me.
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

809 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