Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I need to write a function which will do the following functionalities

I would get a string and a array as input parameters

The string would be like one of the following for example

catalog 
level1Cats
level2Cats

The String array would be containing the data for the strings mentioned above for example

catalog:("12605")

level1Cats:("12605_For the Home") OR level1Cats:("12605_Clothing")

level2Cats:("12605_For the Home_Appliances")

Now if the String is catalog and the array is the one mentioned above then the output should be 12605.

If its is level1Cats then the output should be an array of the following strings

12605_For the Home 
12605_Clothing

I wrote the following code to implement the above mentioned logic It's doing the functionality without a flaw, Can i implement the same in a different and a more efficient way?

public static String[] parseFQ(String fqField, String fqs[]) {

    List<String> prefixes = new ArrayList<String>();

    if(fqField != null && fqField.length() > 0
            && fqs != null && fqs.length > 0) {

        //Get the fq which starts with given field name.
        for(String fq : fqs) {
            if(fq.startsWith(fqField)) {
                fqField = fq;
                break;
            }
        }

        boolean parsed = false;
        while(!parsed) {
            int quoteStart = fqField.indexOf("\"");
            int quoteEnd = (quoteStart >= 0) ? fqField.indexOf("\"", quoteStart+1) : -1;
            if(quoteEnd > 0) {
                prefixes.add(fqField.substring(quoteStart+1, quoteEnd));
                fqField = fqField.substring(quoteEnd+1, fqField.length());
            } else {
                parsed = true;
            }               
        }
    }
    //Return the prefixes array
    return prefixes.toArray(new String[]{});
}
share|improve this question
    
just curiosity, your string contains label and actual value, right? why you can put them into ie map? –  user902383 Jan 23 '13 at 10:55
    
How about using JSON. { name: "12605", children: [{ name: "For the Home", children: [{ name: "Appliances" }] }, { name: "Clothing" }] } –  abuzittin gillifirca Jan 23 '13 at 11:10

1 Answer 1

Below code is more readable and no need of using our logic around single quotes.

String[] w = fqField.split("\"");
for (int i = 1; i < w.length; i+=2) {
    prefixes.add(w[i]);
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.