Results 1 to 11 of 11
  1. #1
    Golfnutest is offline Member
    Join Date
    Oct 2011
    Location
    Washington State
    Posts
    8
    Rep Power
    0

    Default StringIndexOutOfBoundsException

    Hello,

    I've been working on this program for a while now and would very much appreciate a pointer in the right direction.

    The program is designed to read from one document and collect data into an array. This seems to work fine.

    Then the program is intended to read from a document, and if a key word is in the line, to delete it and insert instead an element from the array.

    I keep getting an out of bounds exception at the point in the program where the text is bold / underlined. I ran the program with added code to print the string length and the indexOf value, and it seems to me that it should work.

    If anyone can point me in the right direction, I'd really appreciate it.

    Golfnut

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.FileWriter;
    import java.io.InputStreamReader;
    import java.util.Scanner;
    import java.io.BufferedWriter;

    public class Arrays {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) throws IOException {
    String str = "";
    String str1 = "";

    int pos;
    int pos2;
    int pos3;
    String newfile;
    String newline = System.getProperty ("line.separator");

    String Affidavit[] = new String [50];

    // Create BufferedReader, StringBuilder and Scanner to read Data
    BufferedReader userinput = new BufferedReader (new InputStreamReader(System.in)); // Read from system
    BufferedReader br = new BufferedReader (new FileReader("Return of Service Data.txt")); // Read from data
    FileReader fin = new FileReader("Return of Service Affidavit (1).txt"); // Read from Affidavit template
    Scanner src = new Scanner (fin);
    // Prompt user for filename and create FileWriter
    System.out.println("Please enter a name for the new Affidavit of Service document: ");
    newfile = userinput.readLine(); // store user input to variable newfile
    FileWriter fout = new FileWriter (newfile);
    BufferedWriter bw = new BufferedWriter (fout);
    userinput.close();

    // Create StringBuilder

    StringBuilder sb = new StringBuilder(str);
    StringBuilder sb1 = new StringBuilder(str1);

    // Use for loop to fill array Affidavit [] from "Return of Service Data.txt" document

    for (int i = 0; i <= 1; i++){
    str = br.readLine();
    sb.append(str); // Read line of text from FileReader
    Affidavit[i] = sb.substring(13); // Affidavit [i] initailized to value of substring
    sb.delete(0, sb.length()); // StringBuffer reset to null value
    } // end for loop

    br.close();
    // Create new Affidavit of Service with modifications

    while (src.hasNextLine()){
    str1 = src.nextLine();
    sb1.delete(0, sb.length());
    if (str1.indexOf("PETITIONERNAME") >= 0){
    pos = str1.indexOf("PETITIONERNAME");
    str1 = sb1.delete(pos,14).insert(pos, Affidavit[0]).toString();
    fout.write(str1 + newline);

    }
    else if (str1.indexOf("RESPONDENTNAME") >= 0){
    pos = str1.indexOf("RESPONDENTNAME");
    str1 = sb1.delete(pos,14).insert(pos, Affidavit[1]).toString();
    fout.write(str1 + newline);
    }
    else if (str.indexOf("CASENO") >= 0){
    pos = str1.indexOf("CASENO");
    str1 = sb1.delete(pos, 6).insert(pos, Affidavit[2]).toString();
    fout.write(str1 + newline);
    }
    else if (str1.indexOf("TARGETNAME") >=0){
    pos = str1.indexOf("TARGETNAME");
    str1 = sb1.delete(pos, 10).toString();
    str1 = sb1.insert(pos, Affidavit[3]).toString();
    fout.write(str1 + newline);
    }
    else if (str1.indexOf("DATEOFSERVICE") >= 0){
    pos = str1.indexOf("DATEOFSERVICE");
    pos2 = str1.indexOf("TIMEOFSERVICE");
    str1 = sb1.delete(pos, 13).insert(pos, Affidavit[4]).toString();
    str1 = sb1.delete(pos2, 13).insert(pos2, Affidavit[5]).toString();
    fout.write(str1 + newline);
    }
    else if (str1.indexOf("SERVICEADD1") >= 0) {
    pos = str1.indexOf("SERVICEADD1");
    str1 = sb1.delete(pos, 11).insert(pos, Affidavit[6]).toString();
    fout.write(str1 + newline);
    }
    else if (str1.indexOf("SERVICEADD2") >=0) {
    pos = str1.indexOf("SERVICEADD2");
    str1 = sb1.delete(pos, 11).insert(pos, Affidavit[7]).toString();
    fout.write(str1 + newline);
    }
    else if (str1.indexOf("SERVICECITY") >=0) {
    pos = str1.indexOf("SERVICECITY");
    pos2 = str1.indexOf("SERVICESTATE");
    pos3 = str1.indexOf("SERVICEZIP");
    str1 = sb1.delete(pos, 11).insert(pos, Affidavit[8]).toString();
    str1 = sb1.delete(pos2, 12).insert(pos2, Affidavit[9]).toString();
    str1 = sb1.delete(pos3, 10).insert(pos3, Affidavit[10]).toString();
    fout.write (str1 + newline);
    }

    else if (str1 != null){
    fout.write(str1 + newline);
    }
    else fout.write(newline);



    } // End while loop
    fout.close();
    fin.close();
    } // end main
    } // end class

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: StringIndexOutOfBoundsException

    Use System.out.println() to print the value of sb1 (the string buffer) just before you delete the substring.

    (Not exactly part of your problem, but it seems a bit pointless to assign the string contents to str1 when you assign something else to this variable in the next line.)

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: StringIndexOutOfBoundsException

    Use System.out.println() to print the value of sb1 (the string buffer) just before you delete the substring.

    (Not exactly part of your problem, but it seems a bit pointless to assign the string contents to str1 when you assign something else to this variable in the next line.)

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: StringIndexOutOfBoundsException

    And, please, when you post code use the "code" tags: put [code] at the start of the code and [/code] at the end. This will preserve the formatting when the code appears on a webpage.

  5. #5
    Golfnutest is offline Member
    Join Date
    Oct 2011
    Location
    Washington State
    Posts
    8
    Rep Power
    0

    Default Re: StringIndexOutOfBoundsException

    Thanks pbrockway2. Will do.

  6. #6
    Golfnutest is offline Member
    Join Date
    Oct 2011
    Location
    Washington State
    Posts
    8
    Rep Power
    0

    Default Re: StringIndexOutOfBoundsException

    I did check the value of the StringBuffer and deleted that line of code. As observed, this wasn't my problem: I added that line in an attempt to fix the problem and forgot to remove it.

    Still stuck on this problem if anyone has a suggestion.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: StringIndexOutOfBoundsException

    I did check the value of the StringBuffer
    What was the reported (string) value? And what was the code you used to do that?

  8. #8
    Golfnutest is offline Member
    Join Date
    Oct 2011
    Location
    Washington State
    Posts
    8
    Rep Power
    0

    Default Re: StringIndexOutOfBoundsException

    sb1.length() === 0 until I get an OutOfBoundsException, then it jumps to 11, then 12. Please let me know if anything else would be helpful. Thanks for your help.

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: StringIndexOutOfBoundsException

    Java Code:
    str1 = sb1.delete(pos, 10).toString();
    This line of code attempts delete the contents of sb1 from index pos to just before index 10. If things go wrong I can't do much more than to really really suggest you examine the value that sb1 has at that point. Ie insert code to print the string builder out. If you can't understand what you see then posting the output and the code that produced it will help.

  10. #10
    Golfnutest is offline Member
    Join Date
    Oct 2011
    Location
    Washington State
    Posts
    8
    Rep Power
    0

    Default Re: StringIndexOutOfBoundsException

    Thanks pbrockway2! I think that is going to be very helpful. It helped me to realize that one of the else if loops had a type o (str instead of str1). Will continue to explore and let you know how it goes.

  11. #11
    Golfnutest is offline Member
    Join Date
    Oct 2011
    Location
    Washington State
    Posts
    8
    Rep Power
    0

    Default Re: StringIndexOutOfBoundsException

    Here is where I'm at. I had misunderstood the use of delete(), but for the else if loop using "TARGETNAME", I believe I fixed it. I added some System.out.print lines to see what was going on: the value of pos for the TARGETNAME loop is 56 (line 83). sb1.length() is 15 (line 84).

    Java Code:
    package arrays;
    
    /*
     * @author Ed
     */
    
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.FileWriter;
    import java.io.InputStreamReader;
    import java.util.Scanner;
    
    
    public class Arrays {
    
        public static void main(String[] args) throws IOException {
            String str = "";
            String str1 = "";
            int pos;
            int pos2;
            int pos3;
            String newfile;
            String newline = System.getProperty ("line.separator");
    
            String Affidavit[] = new String [50];
            
            // Create BufferedReader, StringBuilder and Scanner to read Data
            
            BufferedReader userinput = new BufferedReader (new InputStreamReader(System.in));       // Read from system
            BufferedReader br = new BufferedReader (new FileReader("Return of Service Data.txt"));  // Read from data
            FileReader fin = new FileReader("Return of Service Affidavit (1).txt");                 // Read from Affidavit template
            Scanner src = new Scanner (fin);
            
            // Prompt user for filename and create FileWriter
            
            System.out.println("Please enter a name for the new Affidavit of Service document: ");
            newfile = userinput.readLine();                                                         // store user input to variable newfile
            FileWriter fout = new FileWriter (newfile);
            userinput.close();
            
            // Create StringBuilder
         
            StringBuilder sb = new StringBuilder(str);
            StringBuilder sb1 = new StringBuilder (str1);
           
            // Use for loop to fill array Affidavit [] from "Return of Service Data.txt" document
    
                for (int i = 0; i <= 1; i++){
                str = br.readLine();
                sb.append(str);                         // Read line of text from FileReader
                Affidavit[i] = sb.substring(13);        // Affidavit [i] initailized to value of substring
                sb.delete(0, sb.length());              // StringBuffer reset to null value
                } // end for loop
            
            br.close();
            
            // Create new Affidavit of Service with modifications
          
            while (src.hasNextLine()){
                str1 = src.nextLine();
                if (str1.indexOf("PETITIONERNAME") >= 0){
                    pos = str1.indexOf("PETITIONERNAME");
                    System.out.println(sb1.length() + " this is the length for PETITIONERNAME loop");
                    str1 = sb1.delete(pos,14).insert(pos, Affidavit[0]).toString();
                    fout.write(str1 + newline);
                }
                else if (str1.indexOf("RESPONDENTNAME") >= 0){
                    pos = str1.indexOf("RESPONDENTNAME");
                    System.out.println(sb1.length()+ " this is the length for RESPONDENTNAME loop");
                    str1 = sb1.delete(pos,14).insert(pos, Affidavit[1]).toString();
                    fout.write(str1 + newline);
                }
                else if (str1.indexOf("CASENO") >= 0){
                    pos = str1.indexOf("CASENO");
                    System.out.println(sb1.length()+ " this is the length for CASENO loop");
                    str1 = sb1.delete(pos, 6).insert(pos, Affidavit[2]).toString();
                    fout.write(str1 + newline);
                }
                else if (str1.indexOf("TARGETNAME") >= 0){
                    System.out.println(str1.indexOf("TARGETNAME"));
                    pos = str1.indexOf("TARGETNAME");
                    System.out.println(pos + " This is the starting point for the delete operation.");
                    System.out.println(sb1.length()+ " this is the length for TARGETNAME LOOP");
                    str1 = sb1.delete(pos, 66).append(Affidavit[3]).toString();
                    fout.write(str1 + newline);
               }
                else if (str1.indexOf("DATEOFSERVICE") >= 0){ // This else statement will delete and insert 2 substrings on the same line
                    pos = str1.indexOf("DATEOFSERVICE");
                    pos2 = str1.indexOf("TIMEOFSERVICE");
                    str1 = sb1.delete(pos, 13).insert(pos, Affidavit[4]).toString();
                    str1 = sb1.delete(pos2, 13).insert(pos2, Affidavit[5]).toString();
                    fout.write(str1 + newline);
                }
                else if (str1.indexOf("SERVICEADD1") >= 0) {
                    pos = str1.indexOf("SERVICEADD1");
                    str1 = sb1.delete(pos, 11).insert(pos, Affidavit[6]).toString();
                    fout.write(str1 + newline);
                }
                else if (str1.indexOf("SERVICEADD2") >= 0) {
                    pos = str1.indexOf("SERVICEADD2");
                    str1 = sb1.delete(pos, 11).insert(pos, Affidavit[7]).toString();
                    fout.write(str1 + newline);
                }
                else if (str1.indexOf("SERVICECITY") >= 0) {  // This else statement will delete and insert 3 substrings on the same line
                    pos = str1.indexOf("SERVICECITY");
                    pos2 = str1.indexOf("SERVICESTATE");
                    pos3 = str1.indexOf("SERVICEZIP");
                    str1 = sb1.delete(pos, 11).insert(pos, Affidavit[8]).toString();
                    str1 = sb1.delete(pos2, 12).insert(pos2, Affidavit[9]).toString();
                    str1 = sb1.delete(pos3, 10).insert(pos3, Affidavit[10]).toString();
                    fout.write (str1 + newline);
                }
            
                else if (str1 != null){                 // If none of the above apply, write same string originally read
                    fout.write(str1 + newline);
                }
                else fout.write(newline);
            } // End while loop    
            fout.close();
            fin.close();
        } // end main
    } // end class

Similar Threads

  1. Replies: 3
    Last Post: 09-11-2011, 10:15 AM
  2. Replies: 1
    Last Post: 07-06-2011, 08:51 AM
  3. StringIndexOutOfBoundsException- Crossword Solver App.
    By numbskull20 in forum Advanced Java
    Replies: 3
    Last Post: 12-01-2010, 02:33 PM
  4. StringIndexOutOfBoundsException Error
    By Mayur in forum New To Java
    Replies: 4
    Last Post: 02-01-2009, 06:10 PM
  5. StringIndexOutOfBoundsException
    By ravian in forum New To Java
    Replies: 2
    Last Post: 01-30-2008, 12:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •