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 7820779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:25:40+00:00 2026-06-02T07:25:40+00:00

I am new to programming, but I have a need to group by elements,

  • 0

I am new to programming, but I have a need to group by elements, if they are selected by user; therefore, I am not exactly sure how many elements my group by will contain.
Therefore I did this to count amount of selected elements and then, created another statement that creates correct Group by statement that represents the amount of elements needed.

It works, but it looks bulky and I would like to see if there are better ways of doing this?

int totalOutPuts = 0;
totalOutPuts = endbox == true ? totalOutPuts + 1 : totalOutPuts;
totalOutPuts = coursebox == true ? totalOutPuts + 1 : totalOutPuts;
totalOutPuts = departmentbox == true ? totalOutPuts + 1 : totalOutPuts;
totalOutPuts = callbox == true ? totalOutPuts + 1 : totalOutPuts;
totalOutPuts = daybox == true ? totalOutPuts + 1 : totalOutPuts;
totalOutPuts = startbox == true ? totalOutPuts + 1 : totalOutPuts;
totalOutPuts = instructorbox == true ? totalOutPuts + 1 : totalOutPuts;
totalOutPuts = roombox == true ? totalOutPuts + 1 : totalOutPuts;
totalOutPuts = buildingbox == true ? totalOutPuts + 1 : totalOutPuts;
totalOutPuts = numberenrolled == true ? totalOutPuts + 1 : totalOutPuts;
int missingElements = 10 - totalOutPuts;

String groupBy = " Group by 1, 2, 3, 4, 5, 6, 7, 8, 9, 10";
if (missingElements == 9) {
    groupBy = " Group by 1";
} else if (missingElements == 8) {
    groupBy = " Group by 1, 2";
} else if (missingElements == 7) {
    groupBy = " Group by 1, 2, 3";
} else if (missingElements == 6) {
    groupBy = " Group by 1, 2, 3, 4";
} else if (missingElements == 5) {
    groupBy = " Group by 1, 2, 3, 4, 5";
} else if (missingElements == 4) {
    groupBy = " Group by 1, 2, 3, 4, 5, 6";
} else if (missingElements == 3) {
    groupBy = " Group by 1, 2, 3, 4, 5, 6, 7";
} else if (missingElements == 2) {
    groupBy = " Group by 1, 2, 3, 4, 5, 6, 7, 8";
} else if (missingElements == 1) {
    groupBy = " Group by 1, 2, 3, 4, 5, 6, 7, 8, 9";
}
  • 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-06-02T07:25:42+00:00Added an answer on June 2, 2026 at 7:25 am

    Loops! Here’s an implementation suggestion:

    // adding all the vars to an array might improve readability
    boolean checks[] = new boolean[] { endbox, coursebox, departmentbox, callbox,
                                       daybox, startbox, instructorbox, roombox,
                                       buildingbox, numberenrolled };
    
    for (boolean check : checks) {
        if (check) {
            totalOutPuts++;
        }
    }
    
    String groupBy = " Group by ";
    
    // this isn't the best way of appending strings in Java,
    // you'd be better using StringBuilder. this is for the sake of simplicity
    for (int i = 1; i < totalOutPuts; i++) {
        groupBy += i + ", ";
    }
    
    // append the last number
    groupBy += totalOutPuts + "";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Django and Web programming in general. Have googled but could not
I am new to C++ programming, but I have experience in Java. I need
I'm new to Android & Java programming but have been programming in .NET for
I am fairly new to PHP, but have applied my previous knowledge of programming,
I'm new to PHP programming and have poor knowledge about it, but I want
I'm new to C# and don't have any programming experience. But I've finish a
I'm new to OS X programming but generally liking it. I have the following
Disclaimer: I am new to python and django but have Drupal programming experience. I'm
Hi guys I am rather new at AJAX programming but I have managed to
Disclaimer: I am new to python and django but have experience programming in Drupal.

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.