11 Replies Latest reply: Jun 6, 2007 2:07 PM by 807606 RSS

    i have a minor string issue

    807606
      Been looking at the code all day but if I can get a little help this would be great...
      I have a hashtable of buttons and strings and i run a loop to see if they are true...
       public static String checkCaja()
        {
           
             String temp=""; 
             for(String name:checkboxes.keySet()){
                 // System.out.println(checkboxes.get(name).isSelected()+name);
                  if (checkboxes.get(name).isSelected()==true)
                  {   
                       
                       temp=temp+name+",";
                      
                  }
                
             }
           
           return temp;
           
        }
      the temp returns the strings with an ','. So I will get a
      From one,two,three,four, five,

      I would like to remove the last comma from the last word added... any ideas?
      It should look like one, two,three,four,five


      Thank you in advance!
        • 1. Re: i have a minor string issue
          807606
          You could simply add after your loop:
          if(temp.length() > 0) {
              temp = temp.substring(0, temp.length() - 1);
          }
          • 2. Re: i have a minor string issue
            807606
            Or my preferred way:
             public static String checkCaja()
              {
                 
                   String temp="";
                    Iterator<String> it = checkboxes.keySet().iterator();
                   if(it.hasNext()) {
                          String name = it.next();
                        if (checkboxes.get(name).isSelected()==true)
                        {
                             temp=name;
                        }
                   }
                   while(it.hasNext() {
                          String name = it.next();
                        // System.out.println(checkboxes.get(name).isSelected()+name);
                        if (checkboxes.get(name).isSelected()==true)
                        {
                             temp=temp+","+name;
                        }
                      
                   }
                 return temp;
              }
            • 3. Re: i have a minor string issue
              807606
              thanks!
              • 4. Re: i have a minor string issue
                796447
                Or my preferred way:
                ...
                You prefer to duplicate code? It would have been better to just keep track of whether the first item had already been added or not, and add the comma depending on that, rather than duplicate the code block.
                • 5. Re: i have a minor string issue
                  807606
                  Yeah I prefer the duplication (which of course may lead to the creation of a new method so that only the call of that method is duplicated) so that we don't have to bother setting or checking a boolean on every loop for one single action.
                  • 6. Re: i have a minor string issue
                    thomas.behr
                    Yeah I prefer the duplication [...] so that we don't
                    have to bother setting or checking a boolean on every
                    loop for one single action.
                    You might like the following idiom:
                    String del = "";
                    for (...) {
                        sb.append(del).append(...);
                        del = ", ";
                    }
                    • 7. Re: i have a minor string issue
                      807606
                      But then you'll be reassigning ", " to del for every loop even though it doesn't change anything at all.

                      The answer I showed could sure be improved by using StringBuffer and by removing the extra "==true" comparisons, but the code duplication prevents from performing an extra operation inside the loop. Here's one more way of doing it:

                      StringBuffer sb = new StringBuffer();
                      Iterator<String> it = checkboxes.keySet().iterator();
                      if(it.hasNext()) {
                          String name = it.next();
                          if(checkboxes.get(name).isSelected()) {
                              sb.append(name);
                          }
                          while(it.hasNext()) {
                              name = it.next();
                              if(checkboxes.get(name).isSelected()) {
                                  sb.append(", ");
                                  sb.append(name);
                              }
                          }
                      }
                      return sb.toString();
                      • 8. Re: i have a minor string issue
                        807606
                        With this example, if the first checkbox that you check is not selected, then you end up with something like ",two,four,five".
                        > StringBuffer sb = new StringBuffer();
                        
                        Iterator<String> it =
                        checkboxes.keySet().iterator();
                        if(it.hasNext()) {
                        String name = it.next();
                        if(checkboxes.get(name).isSelected()) {
                        sb.append(name);
                        while(it.hasNext()) {
                        name = it.next();
                        if(checkboxes.get(name).isSelected()) {
                        sb.append(", ");
                        sb.append(name);
                        }

                        }
                        return sb.toString();
                        You can check the length of the StringBuffer to determine whether or not to append the comma first:
                        StringBuffer sb = new StringBuffer();
                        Iterator<String> it = checkboxes.keySet().iterator();
                        while(it.hasNext()) {
                            name = it.next();
                            if(checkboxes.get(name).isSelected()) {
                                if (sb.length() > 0) 
                                    sb.append(", ");
                                sb.append(name);
                            }
                        }
                        return sb.toString();
                        • 9. Re: i have a minor string issue
                          807606
                          hmm that's true
                          good thing you pointed this out, hope the OP sees it.
                          • 10. Re: i have a minor string issue
                            807606
                            As long as we are tweaking code, when you're iterating over names and values, you should use the entry set:
                            String f(Map<String, JCheckBox> checkboxes) {
                                StringBuffer sb = new StringBuffer();
                                for(Map.Entry < String, JCheckBox > e : checkboxes.entrySet()) {
                                    if(e.getValue().isSelected()) {
                                        if (sb.length() > 0)
                                            sb.append(", ");
                                        sb.append(e.getKey());
                                    }
                                }
                                return sb.toString();
                            }
                            I would also go with adding the delimiter every time, then substringing to remove the last one, if any:
                            String f(Map<String, JCheckBox> checkboxes) {
                                String DELIM = ", ";
                                StringBuffer sb = new StringBuffer();
                                for(Map.Entry < String, JCheckBox > e : checkboxes.entrySet())
                                    if(e.getValue().isSelected())
                                        sb.append(e.getKey()).append(DELIM);
                                String s = sb.toString();
                                int length = s.length();
                                return length > 0 ? s.substring(0, length - DELIM.length()) : s;
                            }
                            • 11. Re: i have a minor string issue
                              807606
                              wow!!! guess it was not soo simple, lol. Thanks everyone for the input and help it is much appreciated.. Give me a better understanding on what i need to do .