TehNoiseBomb
TehNoiseBomb

Reputation: 41

Need help writing a string to multiple lines in a text file

I have an array of strings toFile[] that I am attempting to write to a text file. While testing, I was reminded that if the sting reaches the bounds of the window, notepad does not wrap the string to the next line. Being a bit of a perfectionist, I want to split the string into substrings that are 250 characters in length and write each of those to the file, breaking into a new line after each substring.

While testing, the problem I have run into, and can't seem to solve, is that my program will run through the loop once and then fails with an error.

An example of the output and error:

toFile[2].length = 2432

temp.length = 250
split = 250
iLength = 2182

temp.length = 0
split = 500
iLength = 1932

java.lang.StringIndexOutOfBoundsException: String index out of range: -250

My code:

System.out.println("toFile[2].length = "+Integer.toString(toFile[2].length()));
System.out.println("");
if(toFile[2].length()>250){
    int iLength=toFile[2].length(), split = 0;
    while(iLength>250){
        String temp = toFile[2];
        temp = temp.substring(split, 250);
        System.out.println("temp.length = "+Integer.toString(temp.length()));
        bw.write(temp);bw.newLine();
        split=split+250;
        System.out.println("split = "+Integer.toString(split));
        iLength=iLength-250;
        System.out.println("iLength = "+Integer.toString(iLength));
        System.out.println("");
    }
    bw.write(toFile[2].substring(split));
}else{bw.write(toFile[2]);bw.newLine();}
bw.newLine();

I have also tried this while loop which runs through the entire string but still only writes the string to one line:

int iLength=toFile[2].length(), start = 0;
String temp = toFile[2];
while(iLength>250){
    bw.write(temp,start,250);

    start=start+250;
    System.out.println("start = "+Integer.toString(start));

    iLength=iLength-250;
    System.out.println("iLength = "+Integer.toString(iLength));
    System.out.println("");
}

Upvotes: 0

Views: 409

Answers (2)

Hitesh Garg
Hitesh Garg

Reputation: 1211

Just correct one thing in your code and I hope that rest of your code will work fine and will not give out the current error. Do the following correction. In the below statement you are fixing the value of end index i.e. 250.

temp = temp.substring(split, 250);

This works fine when you run your code for the first time and stores a string of length 250 in temp because it executes as temp = temp.substring(0, 250); because split=0.

Second time split become 250 and method executes as temp = temp.substring(250, 250); and temp.length goes to 0.

But next time the Begin Index goes beyond End Index i.e temp = temp.substring(500, 250); which is throwing error in your case.

So increase the end index every time you take a substring or you can do.. temp = temp.substring(split, split + 250);

For other interesting and problem solving posts on Java you can visit http://www.codingeek.com/

Upvotes: 1

Tobías
Tobías

Reputation: 6297

First, you need to iterate over the array toFile[] containing all the Strings and handle their writing one by one in another function.

   public static void main (String[] args) throws java.lang.Exception
   {
      String[] toFile = new String[]{ ... };
      BufferedWriter bw = new BufferedWriter(...);

      for (String line : toFile) {   
         write(line, bw);
      }

   }

The next step is to address how to write each String. One way to do it is to write chunks of 250 characters. The only thing you need to check is that the last part can be less than 250, in order to avoid StringIndexOutOfBoundsException

   private static void write(String line, BufferedWriter bw) throws IOException {
      int length = line.length();

      if (length > SPLIT) {         
         int offset = 0;
         while (offset < length) {
            int remaining = length - offset;            
            bw.write(line, offset, remaining < SPLIT ? remaining : SPLIT);
            bw.newLine();            
            offset += SPLIT;
         }         
      } else {         
         bw.write(line);         
         bw.newLine();
      }

   }

And that's all. But if the Strings contains text, splitting in chunks of 250 characters maybe is not the best option since you can cut off words.

Full example code: http://ideone.com/jQKe7Y

Upvotes: 0

Related Questions