1

I want to parse csv like following :

12:50 AM,11.0,10.0,94,1013,10.0,NNW,14.8,-,N/A,,Mostly Cloudy,330,2011-10-27 23:50:00

for parsing it I use a code like below :

while((mylines = rdr.readLine()) != null)
        {   
            Pattern p = Pattern.compile("<br />");
            mylines=mylines.replaceAll("<br />", "");
            System.out.println(mylines);
            StringTokenizer st = new StringTokenizer(mylines , ",");
            while(st.hasMoreTokens())
            {
                System.out.println(st.nextToken());
            }
        }

for the 10th element which I call it Null I don't get any token response which is not good. how can I get token for all the elements?

regards

2
  • Why does your CSV file contain html breaks? Commented Nov 27, 2011 at 13:00
  • what's the expected output and what's the actual output? Commented Nov 27, 2011 at 13:00

6 Answers 6

3

That is the expected behavior of the StringTokenizer class. You can always force your program to return something, "***" for example for empty tokens by adding the following line right before splitting your string:

mylines=mylines.replaceAll(",,", ",***,");

In this case, the StringTokenizer will return *** instead of null

Sign up to request clarification or add additional context in comments.

1 Comment

What if the first value is null?
1

If you're comfortable using a third-party library, use Apache's commons-lang and try StringUtils.splitPreserveAllTokens() - see the API doc.

Comments

0

At the tenth position, you have ,,, which is an empty string, which in Java is represented by a null value. To avoid that the null value is printed, do something like

String str;
if((str = st.nextToken())!=null) {
  System.out.println(str);
} else {
  //print nothing or maybe a newline
}

Comments

0

Hi here is a simple test case that parse the line and gives you all the elements including the empty one:

package com.sg2net.test;

public class TestSplit {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String[] data="12:50 AM,11.0,10.0,94,1013,10.0,NNW,14.8,-,N/A,,Mostly Cloudy,330,2011-10-27 23:50:00§".split(",");
        //String[] data="valore##valore1".split("##");
        System.out.println(data.length);
        if (data.length>0) {
            int i=1;
            for (String element : data) {
                if (element==null || element.length()==0) {
                    System.out.println("empty element at position " + i);
                }
                System.out.println(element);
                i++;
            }
        }

    }

}

Comments

0

Instead of null, look for a token whose length is > 0. In Java, the value null is not the same as an empty string.

Comments

0

ostermiller.org has a jar with nice utility-classes like CSV-Parser/Writer etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.