0

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, e.g 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.

Alex Abdugafarov
6,4827 gold badges39 silver badges60 bronze badges
asked Oct 11, 2011 at 6:48
1
  • 2
    I started to edit this mess but realized I did not know what a ratio button is. DYM JRadioButton? Also do you want to spit a String or split it? For future posts, you may want to review them more carefully, since that question reads like nonsense. Commented Oct 11, 2011 at 6:55

3 Answers 3

3

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.

answered Oct 11, 2011 at 6:55
Sign up to request clarification or add additional context in comments.

Comments

1

columns is an array containing all values of the selected checkboxes. Make sure that the default toString method outputs the items separated by a ","

answered Oct 11, 2011 at 6:54

Comments

1

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);
answered Oct 11, 2011 at 6:59

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.