Question

I have a text-box where i need to give input in variations.Input are for extension column of database table and number are being taken as String.Here are the input conditions:

  1. Only one input like 5028
  2. Input separated by hyphen(-) like 5028-5090.
  3. Input separated by comma(,) like 5028,5029.
  4. Input containing individual,hyphen and comma separated at a single input like 5029,1234-4567,9876.

I have written conditions for formation of query for only one input ,input separated by hyphen,input separated by comma individually ..Now as per my need i have to write conditions where i have to format query for input like 5029,1234-4567,9876 but i am not getting the exact logic ..

Here is my code..

if (extension != "") {
    if (extension.contains(",")) {
        query = query.concat(" and (extension='");
        String extn[] = extension.split(",");
        for (int i = 0; i < extn.length; i++) {
            System.out.println(extn[i]);
            query = query.concat(extn[i]).concat("'").concat(" or extension='");
            System.out.println(query);
        }
        query = query.substring(0, query.length() - 15);
        System.out.println(query);
        query = query.concat(")");
        System.out.println(query);
    } else if (extension.contains("-")) {
        query = query.concat(" and cast(extension as signed) >=");
        String extn[] = extension.split("-");
        for (int i = 0; i < extn.length; i++) {
            System.out.println(extn[i]);
            query = query.concat(extn[i]).concat(" And cast(extension as signed) <=");

        }
        query = query.substring(0, query.length() - 33);
        System.out.println(query);

    } else {
        query = query.concat(" and extension='" + extension).concat("'");
        System.out.println(query);
    }
}

Please guys help me .. Thanks in advance.

Was it helpful?

Solution

Do following changes: 1. replace if (extension != "") with if (!extension.equals(""))

  1. In first if condition block if (extension.contains(",")) after String extn[] = extension.split(","); again split each String extn[] on '-' and do other process

try this

if (!extension.equals("")) {
if (extension.contains(",")) {
    query = query.concat(" and (extension='");
    String extn[] = extension.split(",");
    for(int k=0; k <extn.length; k++){
    if(extn[k].contains("-")){
        String subExtn[] = extn[k].split("-");
        for (int i = 0; i < subExtn.length; i++) {
            //System.out.println(subExtn[i]);
            query = query.concat(subExtn[i]).concat("'").concat(" or extension='");

        }

    }else{
        //for (int i = 0; i < extn.length; i++) {
           // System.out.println(extn[i]);
            query = query.concat(extn[k]).concat("'").concat(" or extension='");
          //  System.out.println(query);
        //}
    }
    //System.out.println(query);
    }
    query = query.substring(0, query.length() - 15);
   // System.out.println(query);
    query = query.concat(")");
    System.out.println(query);
} else if (extension.contains("-")) {
    query = query.concat(" and cast(extension as signed) >=");
    String extn[] = extension.split("-");
    for (int i = 0; i < extn.length; i++) {

        query = query.concat(extn[i]).concat(" And cast(extension as signed) <=");

    }
    query = query.substring(0, query.length() - 33);
    System.out.println(query);

} else {
    query = query.concat(" and extension='" + extension).concat("'");
    System.out.println(query);
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top