2016-03-04 40 views
1

我使用一个构造函数:无效的方法Decleration - 新的方法

Donor(String lastName, String firstName, String type, int age, double minutes) 

而且我知道,我必须命名一样,它驻留在公共类,但是当我将我的花括号使它是由公共捐助者类包含我遇到了几个错误。

我的代码如下:

public class Program5 
{ 
    public static void main(String args[]) 
    { 
     Arrays.sort(Donor, new lastNameComparator()); 
     for (int i = 0; i < donor.length; i++) 
     { 
      System.out.println(Donor[i].getLastName()); 
     } 

     Arrays.sort(Donor, new firstNameComparator()); 
     for (int i = 0; i < donor.length; i++) 
     { 
      System.out.println(Donor[i].getFirstName()); 
     } 

     Arrays.sort(Donor, new typeComparator()); 
     for (int i = 0; i < donor.length; i++) 
     { 
      System.out.println(Donor[i].getType()); 
     } 

     Arrays.sort(Donor, new ageComparator()); 
     for (int i = 0; i < donor.length; i++) 
     { 
      System.out.println(Donor[i].getAge()); 
     } 

     Arrays.sort(Donor, new minutesComparator()); 
     for (int i = 0; i < donor.length; i++) 
     { 
      System.out.println(Donor[i].getMinutes()); 
     } 
    } 

    public ArrayList<Donor> donorCSVList(String filePath) throws IOException 
    { 
     ArrayList<Donor> list = new ArrayList<Donor>(); 
     Scanner scan = new Scanner(new File(filePath)); 
     while (scan.hasNextLine()) 
     { 
      String line = scan.nextLine(); 
      String [] lineArray = line.split(","); 
      list.add(new Donor(lineArray[0], lineArray[1], lineArray[2], lineArray[3], lineArray[4])); 
     } 
    } 

    public class Donor 
    { 
     private String lastName; 
     private String firstName; 
     private String type; 
     private int age; 
     private double minutes; 
    } 

    public void Donor() 
    { 
     super(); 
     type = "Not assigned"; 
     age = 0; 
     minutes = 0.0; 

    } 

    Donor(String lastName, String firstName, String type, int age, double minutes) 
    { 
     super(lastName, firstName); 
     this.lastName = lastName; 
     this.firstName = firstName; 
     this.type = type; 
     this.age = age; 
     this.minutes = minutes; 
    } 

    public String getLastName() 
    { 
     return lastName; 
    } 

    public void setLastName(String lastName) 
    { 
     this.lastName = lastName; 
    } 

    public String getFirstName() 
    { 
     return firstName; 
    } 

    public void setFirstName(String firstName) 
    { 
     this.firstName = firstName; 
    } 

    public String getType() 
    { 
     return type; 
    } 

    public void setType(String type) 
    { 
     this.type = type; 
    } 

    public int getAge() 
    { 
     return age; 
    } 

    public void setAge(int age) 
    { 
     this.age = age; 
    } 

    public double setMinutes() 
    { 
     return minutes; 
    } 

    public void getMinutes(double minutes) 
    { 
     this.minutes = minutes; 
    } 

    public class lastNameComparator implements Comparator 
    { 
     public String compare(Donor o1, Donor o2) 
     { 
      String lastName1 = o1.getLastName(); 
      String lastName2 = o2.getLastName(); 
      return lastName1.compareTo(lastName2); 
     } 
    } 

    public class firstNameComparator implements Comparator 
    { 
     public String compare(Donor o1, Donor o2) 
     { 
      String firsName1 = o1.getFirstName(); 
      String firstName2 = o2.getFirstName(); 
      return firstName1.compareTo(firstName2); 
     } 
    } 

    public class typeComparator implements Comparator 
    { 

     public string compare(Donor o1, Donor o2) 
     { 
      String type1 = o1.getType(); 
      String type2 = o2.getType(); 
      return type1.compareTo(type2); 
     } 
    } 

    public class ageComparator implements Comparator 
    { 
     public int compare(Donor o1, Date o2) 
     { 
      int age1 = o1.getAge(); 
      int age2 = o2.getAge(); 
      //return o2.getAge() - o1.getAge(); 
     } 
    } 

    public class minutesComparator implements Comparator 
    { 
     public double compare(Donor o1, Donor o2) 
     { 
      double minutes1 = o1.getMinutes(); 
      double minutes2 = o2.getMinutes(); 
      //return o2.getMinutes() - o1.getMinutes(); 
     } 
    } 
} 

而且我的错误是:

Program5.java:66: error: invalid method declaration; return type required 
     Donor(String lastName, String firstName, String type, int age, double minutes) 
+0

哪一行是错误?错误消息应该有行号。 –

+0

我已经更新的问题更恰当地反映当前的问题,我现在面临的 – snitchyc

+0

@snitchyc当发布StackOverflow上的示例代码,请您剥去所有不必要的行。 [创建一个简单的例子](http://stackoverflow.com/help/mcve),尽可能少的线仍然可以证明你的问题。作为一种副作用,这样做可以很好地回答你自己的问题。 –

回答

0

你的主要问题是,你放错地方的大部分的Donor类的内容。什么你的结构是这样的:

public class Donor 
{ 
    private String lastName; 
} 

public String getLastName() 
{ 
    return lastName; 
} 

当你想要的东西更像是这样的:

public class Donor 
{ 
    private String lastName; 

    public String getLastName() 
    { 
     return lastName; 
    } 
} 

所有类的成员必须是限定的内容的大括号内上课,而不仅仅是田野。

此外,您似乎要在构造函数中调用super()参数,该构造函数指向的类没有带有带参数的构造函数的超类。这没有意义。

最后你可能是指让你的Donor构造函数public:

public Donor(String lastName, ... 
+0

除Object外的所有类都有超类。为它调用'super()'可能毫无意义,但它是合法的。 – Douglas

+0

我已经移动了包含公共类Donor的结束大括号来保存我所有的getter和setter方法,但这只会增加我的错误。我做错了什么吗? – snitchyc

+0

@Douglas:我猜你在语义上更正确,但我得到的是带参数的'super()'调用。 –

0

人们似乎在这个例子太多错误。我总结了一些:

  • 捐助者类应该在主类之外。另外,它不应该是公共的,Java文件只能有一个公共类(在我们的例子中是Program5)。
  • 所有的比较器类都错误地实现了compare()方法。他们需要使用类型(<T>)参数化,compare()应该有@Override注释。
  • public void Donor()实际上是一种方法。如果我们希望它成为构造函数,那么我们需要删除void
  • super()调用不需要任何构造函数,因为Donor类不扩展任何类。
  • while循环的donorCSVList方法中,我们需要检查lineArray中的元素数量,然后再访问它们的索引。它可能导致arrayIndexOutOfBoundsException
0

Donor是一类,但在main你正在把它当作一个变量来对待。它看起来像你想要用donorCSVList读取一些输入,然后对结果进行排序并输出。要做到这一点,你必须实际调用donorCSVList和使用返回值,就像这样:

ArrayList<Donor> donors = donorCSVList("someFileNameHere"); 
Arrays.sort(donors, new lastNameComparator()); 

...但ArrayList与数组不同,因此Arrays.sort将不会接受它。您需要改用Collections.sort