Need Java Guru help with IO loop

Discussions

EJB programming & troubleshooting: Need Java Guru help with IO loop

  1. Need Java Guru help with IO loop (3 messages)

    The data.txt contains:
    PM|||1|XX|PID|HOT|PLAZA|NO. 4|Main Description|

    The data.txt must change to the data below:
    PM|null|null|1|XX|PID|HOT|PLAZA|NO. 4|Main Description

    I have an error with my loop. Any help why it's not working would be very appreciated?

    import java.io.*;
    import java.util.*;

    public class App8 {

         public App8 ( ) throws IOException{

        BufferedReader br = new BufferedReader(new FileReader(new File("data.txt")));

    int j = 0;
    String rawString = null;
    String modifiedString = null;

    while( (rawString = br.readLine() ) != null)
     {
    //if( (j = rawString.indexOf("||")) > 0 )
    //Changes made here, It does not work, Could you help
    ////////////////// problem here, need a loop here ///////
    while (((j = rawString.indexOf("||")) > 0))
    {
    StringBuffer sb = new StringBuffer(rawString);
    sb.replace(j,j+2,"|null|");
    modifiedString = sb.toString();
         }

    }

    //Verify output (:NB: This will only print out the last value)
    System.out.println(modifiedString);
      
        } //end of constructor

        public static void main ( String args[] ) throws IOException {
                new App8();
        }//End of Main Method


    } // End of Application

    Threaded Messages (3)

  2. Need Java Guru help with IO loop[ Go to top ]

    try change the "=" to "+=" in the while loop, i.e.,
    <b>
    modifiedString += sb.toString();
    </b>

    Feng
    I'm no java guru.
  3. Need Java Guru help with IO loop[ Go to top ]

    It works now. Thanks for the help.
  4. Need Java Guru help with IO loop[ Go to top ]

    I think you should use modifiedString in your inner loop.
    Like this:

    while ((rawString = br.readLine()) != null ) {
      modifiedString = rawString;
      while ((j = modifiedString.indexOf("||")) > 0) {
        StringBuffer sb = new StringBuffer(modifiedString);
        sb.replace(j, j+2, "|null|");
        modifiedString = sb.toString();
      }
      System.out.println("Old line: " + rawString);
      System.out.println("New line: " + modifiedString);
    }

    Andy