简体   繁体   English

Integer.valueOf()错误ArrayIndexOutOfBoundsException:

[英]Integer.valueOf() Error ArrayIndexOutOfBoundsException:

String currentLine = reader.readLine();
while (currentLine != null)
{
  String[] studentDetail = currentLine.split("");

  String name = studentDetail[0];

  int number = Integer.valueOf(studentDetail[1]);
  currentLine = reader.readLine();
}

So I have a file like this: 所以我有一个像这样的文件:

   student1
   student16
   student6
   student9
   student10
   student15

When I run the program said: ArrayIndexOutOfBoundsException:1 当我运行程序时说:ArrayIndexOutOfBoundsException:1

the output should look like this: 输出应如下所示:

   student1
   student6
   student9
   student10
   student11
   student15
   student16

Assuming all the lines start with student and end with a number, you can read all the lines and add them to a list and then sort the list by the number after student , then print each element. 假设所有行都以student开始并以数字结尾,则您可以阅读所有行并将其添加到list ,然后按student之后的数字对list进行sort ,然后print每个元素。 For example: 例如:

String currentLine;
List<String> test = new ArrayList<String>();
while ((currentLine = reader.readLine()) != null)
    test.add(currentLine());
test.stream()
    .sorted((s1, s2) -> Integer.parseInt(s1.substring(7)) - Integer.parseInt(s2.substring(7)))
    .forEach(System.out::println);

output: 输出:

student1
student6
student8
student9

If you don't want to use stream() and lambda , You can sort the list with a custom Comparator then loop through the list and print each element: 如果您不想使用stream()lambda ,则可以使用自定义Comparatorlist进行排序,然后loop list并打印每个元素:

Collections.sort(test, new Comparator<String>() {
    @Override
    public int compare(String s1, String s2) {
        int n1 = Integer.parseInt(s1.substring(7));
        int n2 = Integer.parseInt(s2.substring(7));
        return n1-n2;
    }
});

First, program to the List interface instead of the ArrayList concrete type. 首先,编程到List接口而不是ArrayList具体类型。 Second, use a try-with-resources (or explicitly close your reader in a finally block). 其次,使用try-with-resources (或在finally块中显式关闭reader )。 Third, I would use a Pattern (a regex ) and then a Matcher in the loop to look for "name" and "number". 第三,我将在循环中使用Pattern (一个regex ),然后使用Matcher来查找“名称”和“数字”。 That might look something like, 可能看起来像

List<Student> student = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(new File(infile)))) {
    Pattern p = Pattern.compile("(\\D+)(\\d+)");
    String currentLine;
    while ((currentLine = reader.readLine()) != null) {
        Matcher m = p.matcher(currentLine);
        if (m.matches()) {
            // Assuming `Student` has a `String`, `int` constructor
            student.add(new Student(m.group(1), Integer.parseInt(m.group(2))));
        }
    }
} catch (FileNotFoundException fnfe) {
    fnfe.printStackTrace();
}

Finally, note that Integer.valueOf(String) returns an Integer (which you then unbox ). 最后,请注意Integer.valueOf(String)返回一个Integer (然后将其拆箱 )。 That is why I have used Integer.parseInt(String) here. 这就是为什么我在这里使用Integer.parseInt(String)

Your file must be like this 您的文件必须是这样的

student 1
student 2
student 3

don't forget to add space character between student and number. 不要忘记在学生和数字之间添加空格字符。 And inside your iteration you must add this line: currentLine = reader.readLine(); 在迭代中,您必须添加以下行: currentLine = reader.readLine(); you can split like this: String[] directoryDetail = currentLine.split(" "); 您可以像这样拆分: String[] directoryDetail = currentLine.split(" "); instead of String[] directoryDetail = currentLine.split(""); 而不是String[] directoryDetail = currentLine.split(""); because when you use String[] directoryDetail = currentLine.split(""); 因为当您使用String[] directoryDetail = currentLine.split(""); with student1 the result is a string array of String with length = 0 student1一起 ,结果是一个字符串数组,长度为0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM