This discussion is archived
3 Replies Latest reply: Nov 18, 2012 1:09 AM by Ayham RSS

I can not remove spaces from TEXT file..

Ayham Newbie
Currently Being Moderated
Hi All,
please advise me why the following code not remove the spaces or tab from the text file.

    try
    {
        FileReader fr = new FileReader("D:\\Test\\a.txt");
BufferedReader br = new BufferedReader(fr);

Pattern p;
  Matcher m;
String line;
 String afterReplace = "";
  String inputText = "";
while((line = br.readLine()) != null)
{
   inputText = line;
  p = Pattern.compile("\\s+|\t");
  m = p.matcher(inputText);
  System.out.println(afterReplace);
  afterReplace = afterReplace + m.replaceAll(" ") + "\r\n";
}

  System.out.println(afterReplace);
fr.close();

    }
      catch (Exception e){
  System.err.println("Error: " + e.getMessage());
  }
my regards
Wael
  • 1. Re: I can not remove spaces from TEXT file..
    sabre150 Expert
    Currently Being Moderated
    The replaceAll() in this line
      afterReplace = afterReplace + m.replaceAll(" ") + "\r\n";
    operates on the String containing a single space and replaces that single space with "\r\n". It does not operate on the string referenced by 'afterReplace' so replaces nothing in it.

    P.S. Your regex
    "\\s+|\t"
    is equivalent to
    "\\s+"
    since a tab is a white space!
  • 2. Re: I can not remove spaces from TEXT file..
    Ayham Newbie
    Currently Being Moderated
    many thanks for you , i try to used it for the following text but the results same.


    McNair was shot and killed last weekend in what police say was a murder-suicide.  (July 9)                      Nation / World                          By:  AP                                 
     Comments: text ( 0 ) |  video ( 0 )    + Add a comment        
          Be the first to  add a comment .                                                           
           1          2          3          4          5                 Your Rating               <br>                                                                                                                                                                                                                                                                                                       
        Popularity                                                                                                                                    Your Name:                Your Comment:                                                                                                 
     http://videos.kansascity.com/vmix_hosted_apps/p/media?id=4999903&amp;item_index=&amp;genre_id=00000840 
                                                                     Your Email:                                     Friend's Email:                                     Your Message:                                                                                                                                                                                                                          
    Top Videos                                       Nation / World                       
                    Sports                                       Entertainment                                       Business                                       Lifestyle                                       
    Archive                                       User Submitted                                       Politics                                       Ink                                       Local                                       
    Selects                                                                                                                   
     all                        Sort By:                  Most Recent        Most Popular   
              Highest Rated                                                                 Loading...      
                               <br>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
     All users : If you have not already done so, Click "Record a Comment" to begin. Next, click "Allow" to use your camera.<br><br>  
     Additional Info for Mac users : If you do not see video (after clicking 'allow'), do the following:<br>      Click on the blue "gear" icon   in the lower right corner to bring up the settings window.     Click on the webcam icon.       If you are using a built in iSight camera, 
    choose USB Video Class Video from the pulldown menu. You should see video immediately.<br><br>       Click "close" button.   
    Record! You must record at least ten seconds before 
    you can stop.                
  • 3. Re: I can not remove spaces from TEXT file..
    Ayham Newbie
    Currently Being Moderated
    i have used this
    while((line = br.readLine()) != null)
    {
       inputText = line;
      
      p = Pattern.compile("\\s+");
      m = p.matcher(inputText);
     // System.out.println(afterReplace);
      afterReplace = afterReplace + m.replaceAll("");
    }
     //afterReplace=afterReplace.replaceAll("[\\n\\t ]", "");
      System.out.println(afterReplace);

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points