我没有使用Array的经验,并且在调试控制台中遇到此错误:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
at com.company.SortTextFile.main(SortTextFile.java:28)
我一直在互联网上寻找其他人如何处理StackOverflow中包含的内容,但是我似乎不明白为什么会这样。我试图让该程序从具有20行的多列文本文件中获取输入,如下所示: 爱德华多15 3.9 30000 然后使用collection.sort对其ID进行排序。 我知道数组是0索引,但是我不知道是否需要指定数组大小。
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import static java.lang.Double.*;
public class SortTextFile {
public static void main(String[] args) throws IOException {
// Creating BufferedReader object to read the input text file
BufferedReader reader = new BufferedReader(new FileReader(
"C:\\Users\\miche\\OneDrive\\Documentos\\University\\Algorithms\\Project\\StudentData.txt"));
// Creating ArrayList to hold Student objects
var studentRecords = new ArrayList<Student>();
// Reading Student records one by one
String currentLine = reader.readLine();
while (currentLine != null) {
String[] studentDetail = currentLine.split("\\s+");
String name = studentDetail[0];
int age = Integer.valueOf(studentDetail[1]);
double GPA = valueOf(studentDetail[2]);
int id = Integer.valueOf(studentDetail[3]);
// Creating Student object for every student record and adding it to
// ArrayList
studentRecords.add(new Student(name, age, GPA, id));
currentLine = reader.readLine();
}
// Sorting ArrayList studentRecords based on marks
Collections.sort(studentRecords, new idCompare());
// Creating BufferedWriter object to write into output text file
BufferedWriter writer = new BufferedWriter(new FileWriter(
"C:\\C:\\Users\\miche\\OneDrive\\Documentos\\University\\Algorithms\\Project\\output.txt"));
// Writing every studentRecords into output text file
for (Student student : studentRecords) {
writer.write(student.name);
writer.write(" " + student.age);
writer.write(" " + student.GPA);
writer.write(" " + student.id);
writer.newLine();
}
// Closing the resources
reader.close();
writer.close();
}
}
我安排了一个学生班来比较ID。
public class Student extends SortTextFile {
String name;
int id;
int age;
double GPA;
public Student(String name, int id, double age, double GPA) {
this.name = name;
this.id = id;
this.age = (int) age;
this.GPA = GPA;
}
}
//idCompare Class to compare the marks
class idCompare implements Comparator<Student> {
@Override
public int compare(Student s1, Student s2) {
return s2.id - s1.id;}
}
编辑1: 文本文件仅遵循名称/年龄/ GPA / ID的格式: 奇帕尔多25 3.5 29000
Eduardo 15 3.9 30000
Ricardo 23 3.8 18000
Anthony 24 3.9 19000
Lombardo 29 2.0 22000
Romina 28 2.1 23000
Alex 25 3.1 13000
Sofia 21 2.2 24000
Vexler 24 2.2 25000
Albert 19 3.2 14000
John 24 3.0 15000
Melchor 14 2.9 16000
Bernardo 21 4.0 17000
Diego 19 2.1 26000
Miguelangel 25 2.0 27000
编辑3:我设法在新文件中打印输出。由于某种原因,它是根据年龄而不是ID进行排序的。谢谢您的帮助。我将尝试对该程序实施工具和二进制插入排序,而不是执行Collection.sort谢谢。
如有可能,请尽可能详细地提出任何建议。英语不是我的主要语言,我对此很慢。预先谢谢你
答案 0 :(得分:1)
该消息只是意味着您有一个仅包含1个元素的数组,并且您正在尝试访问数组2个元素。这是计算机科学(和Java语言)中的怪异事物之一,因为我们开始计数从零开始而不是一个,即数组中的第一个元素的索引为studentDetail [0],第二个元素的索引为studentDetail [1]。这就是为什么您会看到令人困惑的“索引1超出长度1的范围”的原因。由currentLine.split(“”)返回的数组仅包含一个字符串,而不是您期望的四个。您需要调试代码以查明发生这种情况的原因(根据您提供的内容,其他人无法回答)。
答案 1 :(得分:0)
您的数组似乎只有一个条目。检查您的string.split(“”)是否存在问题?
答案 2 :(得分:0)
使用currentLine.split("\\s+");
表示字段之间可能存在一个或多个空格或制表符或换行符。
仅当字段之间用一个空格隔开时,您所做的操作才能正常工作。
出于调试目的,使用System.out.println(studentDetail.length);
打印数组的长度
答案 3 :(得分:0)
尝试一下。您没有关闭编写器的代码,这就是为什么输出文件中无内容的原因。
public static void main(String[] args) throws IOException {
//Creating BufferedReader object to read the input text file
BufferedReader reader = new BufferedReader(new FileReader("E:\\Projects\\JavaBasics\\src\\data.txt"));
//Creating ArrayList to hold Student objects
var studentRecords = new ArrayList<Student>();
//Reading Student records one by one
String currentLine = null;
while ((currentLine = reader.readLine()) != null) {
if (!currentLine.isEmpty()) {
System.out.println(currentLine);
String[] studentDetail = currentLine.split(" ");
String name = studentDetail[0];
int age = Integer.valueOf(studentDetail[1]);
double GPA = Double.valueOf(studentDetail[2]);
int id = Integer.valueOf(studentDetail[3]);
studentRecords.add(new Student(name, age, GPA, id));
}
}
//Sorting ArrayList studentRecords based on marks
Collections.sort(studentRecords, new IdCompare());
//Creating BufferedWriter object to write into output text file
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(new File("E:\\Projects\\JavaBasics\\src\\dataout.txt")));
//Writing every studentRecords into output text file
for (Student student : studentRecords) {
System.out.println("Sorted :: " + student.name);
writer.write(student.name);
writer.write(" " + student.age);
writer.write(" " + student.GPA);
writer.write(" " + student.id);
writer.newLine();
}
} finally {
writer.close();
}
}