gpt4 book ai didi

java - 运行文本文档。在cmd中具有多个java类

转载 作者:行者123 更新时间:2023-12-02 11:27:30 25 4
gpt4 key购买 nike

这个程序应该能够按名字、姓氏等进行排序,但是当我在 cmd 中运行该程序时( java Sorting_2 <病人.txt ),我得到了错误(线程“main”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;
}
}

最佳答案

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] 替换为文件的路径,例如“relative/path/to/filename”

关于java - 运行文本文档。在cmd中具有多个java类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49502969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com