• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help with Loop

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic