Reputation: 887
Essentially, wrote a program that reads off values from the following .txt file
P1 0 8
P2 1 4
P3 2 9
P4 3 3
p8 4 5
each line represents a process with its attributes. p1 is a name, 0 is arr_time of p1 and 8 is burst_time of p1 as per the following class:
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--;}
}
I have the following code which is supposed to read and parse the .txt file. Every time it sees a new line it has to add a new process to the priorityQueue prq. It parses and successfully stores the lines of the .txt, but throws a java.lang.ArrayIndexOutOfBoundsException: 1. And I tried tracing it and debugging for hours unsuccessfully. I don't know where it is passing a non-existent index... Here's the problematic code. The error-producing section is in the while loop and I have described it there in a comment.
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]) ));
}
}
}
I am pretty sure (from my tracing) that the problem arises after the first iteration through the while loop. The problem is at the ParseInt operation. And I am completely stuck and confused about it.
Upvotes: 1
Views: 97
Reputation: 15886
Before using array index check the size of array like:
String[] params = line.split(" ");
if(params.length < 3){
// your code if length is not as expected
}
Answer for comment: Before spiting you should add following condition to check if line if empty:
if("".equals(line.trim())){
continue;
}
Upvotes: 5
Reputation: 204
I have checked your code and it runs successfully and gives output. And when I put some white space at starting it is giving me ArrayIndexOutOfBoundsException : 1 error. So, this might be a space problem. I got this output:
reentering while loop
P1
0
8
done
reentering while loop
P2
1
4
done
reentering while loop
P3
2
9
done
reentering while loop
P4
3
3
done
reentering while loop
p8
4
5
done
Upvotes: 2
Reputation: 2053
Wondering if a non-visible /n is throwing you off.
Maybe put a if(params.length > 3) check before you index into it.
If you had a /n you couldn't see, that may cause your array to be smaller.
Upvotes: 1