我正在使用构造函数:
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)
您的主要问题是您错误地放置了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 Donor(String lastName, ...
答案 1 :(得分:0)
此示例中似乎有太多错误。我总结了一些:
compare()
方法。它们需要使用类型(<T>
)进行参数化,compare()
应该@Override
注释。public void Donor()
实际上是一种方法。如果我们希望它是构造函数,那么我们需要删除void
。super()
调用,因为Donor
类不扩展任何类。while
方法的donorCSVList
循环中,我们需要在访问其索引之前检查lineArray
中的元素数量。它可能会导致arrayIndexOutOfBoundsException
。答案 2 :(得分:0)
...&someCollection=System.Collections.Generic[someModel]
是一个类,但在Donor
中,您将其视为变量。看起来你想要的是用main
读取一些输入,然后对其结果进行排序并输出它。要做到这一点,你必须实际调用donorCSVList
并使用返回值,如下所示:
donorCSVList
...除了ArrayList<Donor> donors = donorCSVList("someFileNameHere");
Arrays.sort(donors, new lastNameComparator());
与数组不同,因此ArrayList
不会接受它。您需要改为使用Arrays.sort
。