Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 993369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:24:45+00:00 2026-05-16T06:24:45+00:00

Not sure how to word this algorithm question, actually. I have toggle buttons in

  • 0

Not sure how to word this algorithm question, actually. I have toggle buttons in an android app, each corresponding to a numbered channel. Channels are (1,n) but the toggle button ids are some unknown, unsequential (to the buttons) integer. I need to collect the channel numbers and build a command string. My 1995-based Java skills have given me this:

String ch;
int i = 1;
for (ToggleButton toggle : toggles) {
    if (toggle.isChecked()) {
        ch = String.format("+%d", i+1);
        channels = channels.concat(ch);
    }
} 

If toggle buttons 1,2,4,5,6,7,11,13,21,22,23,24,25 are checked, this code snippet successfully gives me the string "+1+2+4+5+6+7+11+13+21+22+23+24+25"

However, what I would more like to do is have the string "+1/2, +4/7, +11, +13, +21/25"

I wonder if there’s an easier way to do this than multiple if statements:

String ch;
int it = 0;
int last = 1;
for (ToggleButton toggle : toggles ) {
    if (toggle.isChecked()) {
    if (it == last + 1) {
            // somehow continue or note that we're only adding the "/"
        } else {
            // If we're not the next one, then build the last string
            // of "n/last" and then add ", +" to start building next string
        }
    }
    last++;
}

That seems like a bit of brute force type of algorithm, so I don’t know if there’s a more elegant solution that Java might have in it’s bag of tricks (Or, more likely, that I should have)

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T06:24:46+00:00Added an answer on May 16, 2026 at 6:24 am

    On java.util.BitSet

    I’d use java.util.BitSet both for the representation and the span search algorithm. Here’s the basic idea (see on ideone.com):

        import java.util.*;
        //...
    
        BitSet bs = new BitSet();
        int[] onBits = new int[] { 1,2,4,5,6,7,11,13,21,22,23,24,25 };
        for (int onBit : onBits) {
            bs.set(onBit);
        }
        System.out.println(bs);
        // {1, 2, 4, 5, 6, 7, 11, 13, 21, 22, 23, 24, 25}
    
        StringBuilder sb = new StringBuilder();
        for (int begin, end = -1; (begin = bs.nextSetBit(end + 1)) != -1; ) {
            end = bs.nextClearBit(begin) - 1;
            if (sb.length() > 0) sb.append(", ");
            sb.append(
                (begin == end)
                    ? String.format("+%d", begin)
                    : String.format("+%d/%d", begin, end)
            );
        }
        System.out.println(sb);
        // +1/2, +4/7, +11, +13, +21/25
    

    The nextSetBit and nextClearBit methods are really handy in finding the spans.


    On StringBuilder instead of String with +=/concat

    The StringBuilder joining algorithm is a standard one. Here it is when refactored out:

        StringBuilder sb = new StringBuilder();
        for (Element e : elements) {
            if (sb.length() > 0) sb.append(SEPARATOR);
            sb.append(e);
        }
    

    You should use a StringBuilder or StringBuffer instead of String with +=/concat for building long strings.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm really not sure how to word this question, but here goes... I have
Not quite sure how to word this question. I am wondering if there is
I am not really sure how to word this question, so I did the
I am not sure how to really word this, but I have a game
Not really too sure how to word this question, therefore if you don't particularly
I'm not 100% sure how I should word this question but I'll try my
I am not sure whether distinct is the right word for this. I have
I have a question but I'm not sure of the word to use. My
I'm not sure how to word this exactly but I have a script that
I'm not sure the best way to word this question so bear with me.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.