I have a group of radio buttons and a group of checkboxes. I want to print out the selected radio buttons/checked boxes. To print out the radio button selected I use
Object table = radio.getValue();
System.out.println(table);
I get the radio button selected. To get which checkboxes have been selected I use the same:
Object columns = check.getValue();
System.out.println(columns);
I get the checkboxes which have been checked but with square brackets surrounding them, eg if I check the boxes
columnA, columnC, columnF
the printed line would look like this:
[columnA, columnC, columnF]
I want to put the strings of the selected checkboxes into my sql query so I have something like this data.executeQuery("Select " + columns + " From " + table);
It works with the table
but it doesn't work with the columns
.
For this paricular application, you do not need to split a string. You may use
String str = columns.toString(); // "[colummA, colummC, colummF]"
System.out.println(str.substring(1, str.length() - 1));
You may also form a comma-separated string using
StringBuilder sb = new StringBuilder();
for(Object obj : columns) {
if (sb.length() != 0) sb.append(",");
sb.append(obj);
}
However, to answer the question:
String is splitted via split(String)
method using a Regular Expression syntax. As you have a list of items, converting it to a String
and splitting it isnt an effective decision.
columns is an array containing all values of the selected checkboxes. Make sure that the default toString method outputs the items separated by a ","
this is because default
check.getValue().toString()
called and depending on if its a List or a HashSet and default toString() method of List/HashSet is called which by default appends [] while printing the values so for your case you could either use a collection with a overridden toString() method as per your requirement or just write you custom toString method and call it instead of the default toString Method
here is a sample implementation if it is HashSet
private String getHashSetString(Set<String> myset){
StringBuilder sb = new StringBuilder();
Iterator<String> i = myset.iterator();
if (! i.hasNext())
return "";
for (;;) {
Long e = i.next();
sb.append(e);
if (! i.hasNext())
return sb.toString();
sb.append(",");
}
}
So instead of
data.executeQuery("Select " + columns + " From " + table);
write
data.executeQuery("Select " + columns.getHashSetString() + " From " + table);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.