[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 403
  • Last Modified:

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.
0
alexk23
Asked:
alexk23
  • 3
1 Solution
 
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
 
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now