• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with Loop

 
Robert, Assaad
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ganesh Ram
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will not work as
your while (((j = rawString.indexOf("| |")) > 0))
would go in an infinite loop.
You should be checking from a different starting point every time.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!