这个程序应该可以通过firstname lastname ect排序,但是当我在cmd中运行程序时(java Sorting_2< patients.txt)我得到错误(线程中的异常" main&# 34; java.lang.ArrayIndexOutOfBoundsException:0 在Sorting_2.main(Sorting_2.java:10)) 我做错了什么?
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, insuranceCompany, age, 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();
}
}
}
其他计划
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 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;
}
}
答案 0 :(得分:1)
main方法接受一个名为args的参数。这应该在程序的运行配置中定义。 Args是一个字符串数组。您正在调用数组中的第一个值args [0]。这需要一个长度至少有一个值的数组,args.length()等于1.
所以要么你必须定义args。有关更多信息,请参阅此 What is "String args[]"? parameter in main method Java,https://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html。
或者用文件路径替换args [0],例如&#34; relative / path / to / filename&#34;
答案 1 :(得分:0)
我忘记了arg(java Sorting_2 test.txt lastName)或(java Sorting_2 test.txt firstName)等。