Results 1 to 4 of 4
  1. #1
    phosphide is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default StringTokenizer Skips Empty Spaces in CSV File

    Hi all,

    I'm currently using the StringTokenizer class to import csv data into a program I am writing. However, I have noticed that the function nextToken() skips over empty spaces in the csv data.

    Example CSV Data with empty position:

    Java Code:
    John, Doe, Nike, 1000 Death Valley Drive, , Dallas, Tx, 77777
    Will return an array of strings with:

    Java Code:
    John
    Doe
    Nike
    1000 Death Valley Drive
    Dallas
    Tx
    77777
    Where as I really need that empty position to be in the array as well. Does anyone have a suggestion? Because it appears to be simply skipping over the empty spaces and I can't even call for a null space. I tried doing something like:

    Java Code:
    String s = st.nextToken();
    if(s.isEmpty()){
    }
    But doesn't appear to work.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: StringTokenizer Skips Empty Spaces in CSV File

    A StringTokenizer should not skip the token in your String above unless it has been set up incorrectly. Perhaps you're using ", " as the delimiter instead of ","?

    When I tested it, it works fine:

    Java Code:
          String test = "John, Doe, Nike, 1000 Death Valley Drive, , Dallas, Tx, 77777";
          
          StringTokenizer tokenizer = new StringTokenizer(test, ",");
          while (tokenizer.hasMoreElements()) {
             String nextElement = tokenizer.nextElement().toString().trim();
             System.out.println(nextElement );
          }
    But having said this, I strongly urge you to use String#split(...) instead of a Stringtokenizer as this will give you the power of regular expressions.
    Last edited by Fubarable; 06-23-2012 at 10:13 PM.

  3. #3
    phosphide is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default Re: StringTokenizer Skips Empty Spaces in CSV File

    Sorry, I didn't put the csv data correctly as it shows up in a csv document. It appears like so:

    Java Code:
    John,Doe,Nike,1000 Death Valley Drive,,Dallas,Tx,77777
    I updated your code.

    Java Code:
    String test = "John,Doe,Nike,1000 Death Valley Drive,,Dallas,Tx,77777";
      StringTokenizer tokenizer = new StringTokenizer(test, ",");
      while (tokenizer.hasMoreElements()) {
           String nextElement = tokenizer.nextElement().toString().trim();
           System.out.println(nextElement );
    }
    I got the following:

    Java Code:
    John
    Doe
    Nike
    1000 Death Valley Drive
    Dallas
    Tx
    77777
    So it appears to skip through the empty comma.

  4. #4
    phosphide is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default Re: StringTokenizer Skips Empty Spaces in CSV File

    Just posting since I was in the middle of testing that string.split(",") retains the whitespace. Looks like I will be utilizing this instead.

Similar Threads

  1. Replies: 4
    Last Post: 06-23-2012, 09:39 AM
  2. Replies: 18
    Last Post: 01-19-2011, 07:29 PM
  3. How do I count empty spaces in a byte array?
    By nessa203 in forum New To Java
    Replies: 13
    Last Post: 01-11-2010, 05:46 PM
  4. Delete Empty Spaces...
    By ohytheng in forum New To Java
    Replies: 1
    Last Post: 04-15-2009, 09:59 PM
  5. Reading file data that contains no spaces
    By jdepue in forum Advanced Java
    Replies: 1
    Last Post: 08-01-2007, 04:58 AM

Posting Permissions

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