Parse string using StringTokenizer

Hello I am trying to tokenize String by comma delimiters but there is the problem in the line come of the values included in double quotes and contain comma in side the quotation:
 //for example
           String[] value = {"\"234\", testZ\", 123, \"testY, 546\"", "ppp, zzz, qqq"};
            for(int z=0; z<value.length; z++)
            {
               
                StringTokenizer tokenizer = new StringTokenizer(value[z],"\",", true);
                for( int i=0; tokenizer.hasMoreTokens(); i++ )
                {
                    String token = tokenizer.nextToken().trim();
                    //System.out.println(token);


                }
            }

I need to get output somethink like this:
/*
   //First Line -------------------------------
    234", testZ
    123
    testY, 546"

     //Second line--------------------------------
     ppp
     zzz
     qqq
 */

Thank you.
alexk23Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

imladrisCommented:
So the string the program will see is:

"234", testZ", 123, "testY, 546"

Given that string there is no apparent way to determine that the double quotes after the 4 are part of the string. Purely based on double quotes, the strings would be:

234
, 123,

If it were escaped you could presumably manage:

"234\", testZ", 123, "testY, 546"

To produce that string internally you would have to write:

"\"234\\\", testZ\", 123, \"testY, 546\""

Given that you could set the stringtokenizer to return tokens (constructor:  public StringTokenizer(String str,String delim,boolean returnTokens); ) and use delimiters of comma and double quotes.

You would then have to process each token. You would then break strings on comma *unless* you have only processed one of a pair of double quotes, otherwise tokens would be appended.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
imladrisCommented:
P.S. You would need to process the escape character (backslash) as well.

Code for that would be something like:


           String[] value = {"\"234\", testZ\", 123, \"testY, 546\"", "ppp, zzz, qqq"};
            String outstr=""
            boolean instring=false;
            boolean escp=false;
            for(int z=0; z<value.length; z++)
            {
               
                StringTokenizer tokenizer = new StringTokenizer(value[z],"\",\\", true);
                for( int i=0; tokenizer.hasMoreTokens(); i++ )
                {
                    String token = tokenizer.nextToken().trim();
                    if(token.equals("\\"))
                    {   if(escp)
                        {   outstr+="\\";
                            escp=false;
                        }
                        else escp=true;
                    }
                    else if(token.equals("\""))
                    {   if(escp)
                        {   outstr+="\"";
                             escp=false;
                        }
                        else instring=!instring;
                    }
                    else if(token.equals(","))
                    {   if(instring)outstr+=",";
                         else
                         {   System.out.println(outstr);
                              outstr="";
                         }
                     }
                }
            }


OK, well, I wrote that off the top of my head, so it may not be perfect. But I've done a lot of these things over the years; it should be pretty close. I assume you will see the intent. Feel free to ask for any clarification.
0
imladrisCommented:
Did that help?

If so, then it is now time to grade and close this.

If not, then perhaps a clarifying question would help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.