10 Replies Latest reply on Oct 25, 2010 7:23 PM by 796440

    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();
      //--------------------------------------------< 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);

      } catch (IOException e) {
      // catch possible io errors from readLine()
      System.out.println("Uh oh, got an IOException error!");

      } // 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 LESCIA SHEVER 19581113 20030618
      52668163008 RANDAL IWAMATO 19700803 20050618
      Exception in thread "main"
        • 1. Re: java.lang.StringIndexOutOfBoundsException-urgent plz.
          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" );
          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().
          • 2. Re: java.lang.StringIndexOutOfBoundsException-urgent plz.
            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!
            • 3. Re: java.lang.StringIndexOutOfBoundsException-urgent plz.
              i gotta rite a program for my comp java class, and it dont work and i have no idea wats wrong . some1 help me out plz. its supposed to input a string and print it out reversed, but i cant get it to work. it compiles fine, but gives a runtime error
              import java.util.Scanner;
              public class StringUtil
                   public static void main(String[]args)
                        StringUtil test = new StringUtil();
                   public void reversal()
                        Scanner in = new Scanner(System.in);
                        String str = in.next();
                        int x = str.length();
                        int y = 0;
                        while (x>y)
                             int z = x-y;
              if i input "jkjj", this gives me
              Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4
                  at java.lang.String.charAt(String.java:687)
                  at StringUtil.reversal(StringUtil.java:24)
                  at StringUtil.main(StringUtil.java:7)
              Process completed.
              pleaz help. thanx in advance

              Edited by: meee on May 7, 2008 8:28 AM
              • 4. Re: java.lang.StringIndexOutOfBoundsException-urgent plz.
                never mind i got it figured out
                • 5. Re: java.lang.StringIndexOutOfBoundsException-urgent plz.
                  What were you doing wrong? I am getting the same error for a similar program I am trying to write and cannot figure out why.
                  • 6. Re: java.lang.StringIndexOutOfBoundsException-urgent plz.
                    suz62 wrote:
                    I am getting the same error for a similar program I am trying to write and cannot figure out why.
                    I doubt the first.

                    Don't add new questions to old threads.
                    And post complete questions.
                    • 7. Re: java.lang.StringIndexOutOfBoundsException-urgent plz.
                      Hello Everyone,

                      I am getting same error as

                      java.lang.StringIndexOutOfBoundsException: String index out of range: 0
                      at java.lang.String.charAt(String.java:558)

                      Here is the code:

                      String resourceNo[] = p_request.getStringValues(RESOURCENO);
                      int resLength = resourceNo.length ;
                      String unitSerial[] = getReqStringValues(p_request,UNITSERIAL,resLength) ;
                      String numOfUnit[] = getReqStringValues(p_request,UNITNO,resLength) ;
                      String unitType[] = getReqStringValues(p_request,UNITTYPE,resLength) ;
                      String allowsplit[] = getReqStringValues(p_request,ALLOWSPLIT,resLength) ;
                      String moveunit[] = getReqStringValues(p_request,MOVEUNIT,resLength) ;
                      String processallocation[] = getReqStringValues(p_request,PROCESSALLOCATION,resLength) ;
                      String confirmallocation[] = getReqStringValues(p_request,CONFIRMALLOCATION,resLength);

                      char uType ='E';
                      double NoOfUnits = 0;
                      double SConversion = 0;
                      double LConversion = 0;
                      double PConversion = 0;
                      double DConversion = 0;
                      AllocateProductStat allocPrd = null;
                      for (int i = 0; i < resourceNo.length; i++) {
                      if((resourceNo.trim()!= null) && !(resourceNo[i].equals(" "))&& (resourceNo[i].trim().length()>1)){                 
                      allocPrd = new AllocateProductStat();
                      allocPrd.setAgainstInv(YES) ;
                      uType = unitType[i].charAt(0);
                      ProductSingleton productRetriever = ProductSingleton.getInstance() ;
                                     Product prod = productRetriever.getProduct(resourceNo[i].trim());
                                     ProductPackage productPkg = null;
                                     try {
                                          productPkg = prod.getPackaging() ;
                                          SConversion = productPkg.getLamps(ProductUnit.CARTON,1) ;
                                          LConversion = productPkg.getLamps(ProductUnit.LAYER,1) ;
                                          PConversion = productPkg.getLamps(ProductUnit.PALLET,1) ;
                                          DConversion = productPkg.getLamps(ProductUnit.DOUBLE,1) ;
                                     } catch (LazyUpdateIncompleteException e) {
                                          // TODO Auto-generated catch block

                      I am not sure why it is coming there need help ASAP
                      Thanks for all your effort
                      • 8. Re: java.lang.StringIndexOutOfBoundsException-urgent plz.
                        Oak99 wrote:
                        I am getting same error as
                        The same error does not mean it is the same question.
                        Don't add new questions to old threads.

                        And post your question in the New to Java forum.
                        • 9. Re: java.lang.StringIndexOutOfBoundsException-urgent plz.
                          All these errors illustrate the same basic principle: If you don't have absolute control over your input data, then check it thoroughly for completeness before you try to manipulate it. That advice applies to text files, database records, data coming into a socket or anything else your app didn't create itself.

                          Wish I had a nickel for each time I failed to do that before I wised up.
                          • 10. Re: java.lang.StringIndexOutOfBoundsException-urgent plz.
                            1. Do not mark your question as "urgent", no matter how urgent it may be to you. It is not the least bit urgent to anybody here, and nobody cares a whit about your time pressures. The only possible effect that marking your question as urgent could have is to put people off who might otherwise have been interested in helping you..

                            2. To effectively communicate with those whose help you seek, use real words like "please," not SMS non-words like "plz."

                            3. When posting code, surround it with [ code ] and [ /code ] (without the spaces inside the brackets) to make it readable. Use the Preview tab to see what your post will look like before submitting.