java.lang.StringIndexOutOfBoundsException-urgent plz.

import java.io.*;

import java.lang.String.*;

import java.util.*;

public class ReadFile {

//--< main >--//

public static void main (String[] args) {

ReadFile t = new ReadFile();

t.readMyFile();

}

//--< readMyFile >--//

void readMyFile() {

String line = null;

String dcn = null;

String pfn = null;

String pln = null;

String pdob = null;

String ssd = null;

try {

FileReader fr = new FileReader("C:\\ClaimsData\\ClaimsExtract.txt");

BufferedReader br = new BufferedReader(fr);

line = new String();

while ((line = br.readLine()) != null) {

//A00002314376A5272201102300000000MASARU OKUDA 1933012520050722B101 20051001

//Each line of ClaimsExtract.txt is exactly in above form

// and there are about 1000 such lines

dcn = line.substring(13,24);

pfn = line.substring(32,42);

pln = line.substring(42,57);

pdob = line.substring(57,65);

ssd = line.substring(65,73);

//System.out.println(dcn+" "+pfn+" "+pln+" "+pdob+" "+ssd);

}

fr.close();

} catch (IOException e) {

// catch possible io errors from readLine()

System.out.println("Uh oh, got an IOException error!");

e.printStackTrace();

}

} // end of readMyFile()

} // end of class

When I execute this class, I get the following error just before the last two lines are displayed. Can anyone please tell me a workaround for this?

java.lang.StringIndexOutOfBoundsException: String index out of range: 24

at java.lang.String.substring(String.java:1441)

at ReadFile.readMyFile(ReadFile.java:34)

at ReadFile.main(ReadFile.java:11)

52650143086 LESCIASHEVER 19581113 20030618

52668163008 RANDALIWAMATO 19700803 20050618

Exception in thread "main"

[1882 byte] By [babusai1a] at [2007-10-2 20:14:40]
# 1

At a guess the last line in the file is a blank line and the returned string is either zero or one character long. Add something like:

while ((line = br.readLine()) != null) {

if( line.length < 74 ) {// or whatever the max length of a line is

System.err.println( "read a line that was too short" );

continue;

}

Maybe you don't need to print an error - I don't know what your requirements are. But either way you should verify that you got enough data before trying to do the substring().

stdunbara at 2007-7-13 22:56:51 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 2

That was like the Master of Java

Thanks! a Million

The extract file did had the last line with only one character

I deleted the last line and it gave correct results without displaying any error

These Forums are like God blessings only because of guys like you

Thanks a lot!

babusai1a at 2007-7-13 22:56:51 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...