Results 1 to 5 of 5
  1. #1
    trl
    trl is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default Trim() doesn't trim()

    Hello,

    I'm trying to read comma separated data but there are some blank spaces wreaking havoc. So I tried the trim() method to remove the spaces since from the book it looked straight forward. Not.

    Here's what I thought would work (just to remove the spaces):

    File file0 = new File("DataFile.txt");

    Scanner readFromFile = new Scanner(file0);//open from file
    String fileLineOld,
    fileLineNew1;

    while(readFromFile.hasNextLine())
    {

    fileLineOld = readFromFile.nextLine();//read from file
    System.out.println(fileLineOld);

    fileLineNew1 = fileLineOld.trim();
    System.out.println(fileLineNew1);

    }//end while


    Here's the output:

    (a space here)950, 624, 27.4, 21.4, 70, 6.0, 23.0,264, 4,305,308,298,356,17.13
    950, 624, 27.4, 21.4, 70, 6.0, 23.0,264, 4,305,308,298,356,17.13

    It gets rid of the first space but nothing else.

    What am I missing.

    Thanks for any help.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    What was in the DataFile.txt?
    What do you expect the trim() method to do?
    To see if there are leading or trailing spaces in a String when you print it add delimiters:
    System.out.println(">"+ fileLineNew1 + "<");

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    The trim method removes leading and trailing spaces. Nothing else. If you have spaces in the middle of the String then you need to handle them yourself.
    Last edited by Junky; 08-29-2011 at 02:48 AM.

  4. #4
    trl
    trl is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    The trim method removes leading and trailing spaces. Nothing else. If you have spaces in the middle of the String then you need to handle them yourself.
    Oh brother. I was afraid you were going to say that...

    Norm, fileLineOld has one record. I just used println to compare the differences. It was just an experiment to see what would work. Ultimately, I'll have to replace each record with "spaceless" records into a new file or rewrite to the original file (that makes me nervous even though I have many copies). And then do the other stuff.

    Thanks guys.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default

    Go through the Strnig API. I can think of two methods that can do what you want. One uses a regex, the other doesn't.

    db

Similar Threads

  1. Trim for null string
    By laosu in forum New To Java
    Replies: 3
    Last Post: 02-08-2011, 06:56 AM
  2. String.trim() method help.....
    By arson09 in forum New To Java
    Replies: 6
    Last Post: 04-23-2010, 01:19 AM
  3. How to trim the value in jrxml code
    By Deepa in forum New To Java
    Replies: 12
    Last Post: 01-03-2009, 08:27 AM
  4. How to trim text in viewer
    By diva_garg in forum SWT / JFace
    Replies: 4
    Last Post: 08-26-2008, 02:03 PM
  5. String trim
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-21-2008, 04:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •