I am writing code to take input from a file which has records in a given format and then splitting them to display pincodes which i need to compare later and write the records. To do the comparision i am first taking all the pincode fields in the records and storing them in a array.I am able to display pincodes but there is an exception. My method code is below.
String[] getPincodesArray(String filename) {
String[] st=new String[30];
int index=0;
try {
FileReader fr=new FileReader(filename);
BufferedReader br=new BufferedReader(fr);
String line=br.readLine();
while(line!=null) {
String s[]=line.split(",");
String s1[]=s[1].split(";");
st[index]=s1[1];
index++;
line=br.readLine();
}
}
catch(Exception e) {
e.printStackTrace();
}
return st;
}
when i am executing this function is main(),I am getting an exception but am able to display all pincodes.The output is
java.lang.ArrayIndexOutOfBoundsException: 1
at info.pack.Methods.getPincodesArray(Methods.java:270)
at info.pack.studentidpincode.main(studentidpincode.java:18)
560054 560076 560098 560054 560097 560087 560054 null null null..
I am able to see all pincodes, then why is the exception coming?