无法弄清楚什么是错的。真的很好,相同类型的副本,但也许我错过了它。如果它是重复的话,还是真的很新,很抱歉。
Sorting_2.java:25:错误:不兼容的类型:int无法转换为String 排序排序=新排序(lastName,firstName,age,insuranceCompany,roomNum); ^ 注意:某些消息已经简化;使用-Xdiags重新编译:详细以获得完整输出 1错误
import java.io.*;
import java.util.*;
public class Sorting
{
public String lastName;
public String firstName;
public int age;
public String insuranceCompany;
public int roomNum;
//public String toString()
//{
//return name + " " + population + " " + state;
//return String.format("%-13s %-17s %9d", lastName, firstName, age, insuranceCompany,roomNum);
//}
public Sorting(String l, String f, String i, int a, int r)
{
lastName = l;
firstName = f;
age = a;
insuranceCompany = i;
roomNum = r;
}
}
class lastNameComarator implements Comparator<Sorting>
{
public int compare(Sorting a, Sorting b)
{
return a.lastName.compareToIgnoreCase(a.lastName);
}
}
class firstNameComparator implements Comparator<Sorting>
{
public int compare(Sorting a, Sorting b)
{
return a.firstName.compareToIgnoreCase(b.firstName);
}
}
class insuranceCompanyComparator implements Comparator<Sorting>
{
public int compare(Sorting a, Sorting b)
{
return a.insuranceCompany.compareToIgnoreCase(b.insuranceCompany);
}
}
class ageComparator implements Comparator<Sorting>
{
public int compare(Sorting a, Sorting b)
{
if (a.age < b.age) return -1;
else if (b.age < a.age) return 1;
else return 0;
}
}
class roomNumComparator implements Comparator<Sorting>
{
public int compare(Sorting a, Sorting b)
{
if (a.roomNum < b.roomNum) return -1;
else if (b.roomNum < a.roomNum) return 1;
else return 0;
}
}
另一个程序
import java.io.*;
import java.util.*;
public class Sorting_2
{
public static void main(String[] args)
{
try
{
String fileName = args[0];
ArrayList<Sorting> patient = new ArrayList<Sorting>();
Scanner scan = new Scanner ( new File (fileName) );
while (scan.hasNext())
{
String line = scan.nextLine();
String[] tokens = line.split(",");
String lastName = tokens[0];
String firstName = tokens[1];
int age = Integer.parseInt(tokens[2]);
String insuranceCompany = tokens[3];
int roomNum = Integer.parseInt(tokens[4]);
Sorting sorting = new Sorting(lastName, firstName, age,
insuranceCompany, roomNum);
patient.add(sorting);
}
String comparisonType = args [1];
if (comparisonType.equals("lastName"))
{
Collections.sort(patient, new lastNameComarator());
}
else if (comparisonType.equals("firstName"))
{
Collections.sort(patient, new firstNameComparator());
}
else if (comparisonType.equals("age"))
{
Collections.sort(patient,new ageComparator());
}
else if (comparisonType.equals("insuranceCompany"))
{
Collections.sort(patient, new insuranceCompanyComparator());
}
else if (comparisonType.equals("roomNum"))
{
Collections.sort(patient, new roomNumComparator());
}
System.out.println();
System.out.printf("%-13s %-17s %-9s %-10s %9s%n", "lastName", "firstName", "age", "insuranceCompany", "roomNum");
for(Sorting c : patient)
{
System.out.println(c);
}
}
catch (FileNotFoundException ex)
{
ex.printStackTrace();
}
}
}
答案 0 :(得分:3)
构造函数参数的顺序是错误的。你的签名以两个整数结尾,前面有三个字符串;您需要交换insuranceCompany
和age
。像,
Sorting sorting = new Sorting(lastName, firstName, insuranceCompany, age, roomNum);