Reputation: 483
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:
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.
Upvotes: 0
Views: 108
Reputation: 1315
Do following changes:
1. replace if (extension != "")
with if (!extension.equals(""))
if (extension.contains(","))
after String extn[] = extension.split(",");
again split each String extn[] on '-' and do other processtry 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);
}
}
Upvotes: 1