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
-
Need Java Guru help with IO loop (3 messages)
- Posted by: Web Master
- Posted on: August 27 2001 13:34 EDT
Threaded Messages (3)
- Need Java Guru help with IO loop by Jay Feng on August 27 2001 15:33 EDT
- Need Java Guru help with IO loop by Web Master on September 04 2001 13:25 EDT
- Need Java Guru help with IO loop by Andy Nguyen on August 29 2001 10:40 EDT
-
Need Java Guru help with IO loop[ Go to top ]
- Posted by: Jay Feng
- Posted on: August 27 2001 15:33 EDT
- in response to Web Master
try change the "=" to "+=" in the while loop, i.e.,
<b>
modifiedString += sb.toString();
</b>
Feng
I'm no java guru. -
Need Java Guru help with IO loop[ Go to top ]
- Posted by: Web Master
- Posted on: September 04 2001 13:25 EDT
- in response to Jay Feng
It works now. Thanks for the help. -
Need Java Guru help with IO loop[ Go to top ]
- Posted by: Andy Nguyen
- Posted on: August 29 2001 10:40 EDT
- in response to Web Master
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