java - ArrayIndexOutOfBoundsException : 1. 我的索引有什么问题? java

标签 java

基本上,编写了一个从以下 .txt 文件中读取值的程序

P1 0 8
P2 1 4
P3 2 9
P4 3 3
p8 4 5

每一行代表一个进程及其属性。 p1 是一个名称,0 是 p1 的 arr_time,8 是 p1 的 burst_time,根据以下类:

public class Process {

    private String name;
    private int arrive_time= 0;
    private int burst_time = 0;
    private int remain_time = 0;

    public Process (String name, int arr_time, int bur_time) {

        this.arrive_time = arr_time;
        this.burst_time = bur_time;
        this.remain_time = burst_time;
        this.name = name;
    }

    public int getArrTime() {return arrive_time;}
    public int getBurTime() {return burst_time;}
    public int getRemTime() {return remain_time;}
    public String getName() {return name;}

    public void decRemTime() {this.remain_time--;}
}

我有以下代码应该读取和解析 .txt 文件。每次它看到一个新行时,它必须向 priorityQueue prq 添加一个新进程。它解析并成功存储了 .txt 的行,但抛出 java.lang.ArrayIndexOutOfBoundsException: 1。我尝试跟踪它并调试了几个小时,但没有成功。我不知道它在哪里传递了一个不存在的索引……这是有问题的代码。产生错误的部分在 while 循环中,我已在评论中对其进行了描述。

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

public class Test {

    //Priority READY_QUEUE for the accessed processes
    public static PriorityQueue<Process> prq = new PriorityQueue<Process>(5, new Comparator<Process> () {

        @Override
        public int compare(Process p1, Process p2) {
            return p1.getArrTime() - p2.getArrTime();
        }
    });

    public static void main(String[] args) throws IOException {

        BufferedReader br = null;
        String line;

        try {
            br =  new BufferedReader(new FileReader("C:\\Users\\Veni\\Desktop\\test\\test.txt\\"));
        }
        catch (FileNotFoundException fnfex) {
            System.out.println(fnfex.getMessage() + "File not found");
            System.exit(0);
        }

        /*
         * We get an error here in the ParseInt operation
         * immediately following the first while loop iteration
         */ 
        while((line = br.readLine()) != null) {

            System.out.println("reentering while loop");

            String[] params = line.split(" ");

            System.out.println(params[0]);
            System.out.println(Integer.parseInt(params[1]));
            System.out.println(Integer.parseInt(params[2]));
            System.out.println("done");

            prq.add(new Process(params[0], Integer.parseInt(params[1]), Integer.parseInt(params[2]) ));
        }
    }
}

我很确定(根据我的跟踪)问题是在 while 循环的第一次迭代之后出现的。问题出在 ParseInt 操作上。我对此完全陷入困境和困惑。

最佳答案

在使用数组索引之前检查数组的大小,如:

String[] params = line.split(" ");
if(params.length < 3){
  // your code if length is not as expected
}

评论答案:吐出之前你应该添加以下条件来检查行是否为空:

if("".equals(line.trim())){
  continue;
}

关于java - ArrayIndexOutOfBoundsException : 1. 我的索引有什么问题? java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29620006/

相关文章:

java - 创建 Intellij IDEA 插件。存储插件设置

Java 8、Base64.Decoder编译

java - 读取文件时出错 : java. io.FileNotFoundException:打开失败:ENOENT(没有此类文件或目录)

java - 如何解释这个运算符结合性?

java - Facebook AnE Android : Login Activity - . ApiException:代码 1:发生未知错误

java - 尝试读取图像并出现空指针异常

java - 如果使用空格作为正则表达式的分隔符,如何处理值中的空格?

java - 我可以在不分发源代码的情况下将 JavaFX 2.x 用于我的应用程序吗?

java - 枚举的大小 <String>

Java 8 减去日期返回 1 天