Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

logic cleanup help needed in java

I am working in java and came across an odd block of code that my fellow college wrote i'm sure the modulus operator would work but for some reason i'm not getting the expected results i thought i would from the operator.

right now the code is written as follows:

long divisionID = myMaxId % 40;
                if (divisionID == 0) {
                    divisionID = 1;
                }
                long empiresubID = 1;
                if (myMaxId >= 1600) {
                    empiresubID = 2;
                }
                if (myMaxId >= (1600 * 2)) {
                    empiresubID = 3;
                }
                if (myMaxId >= (1600 * 3)) {
                    empiresubID = 4;
                }
                if (myMaxId >= (1600 * 4)) {
                    empiresubID = 5;
                }
                if (myMaxId >= (1600 * 5)) {
                    empiresubID = 6;
                }

it's like this for a few other places and this goes up through 1600*40.

i thought using the modulus operator for myMaxId %1600 but that gave me way incorrect results.

the end goal is there are 40 divisions, we first fill sub division 1 for all divisions first using myMaxID % 40 as users register.

then once that is done the subdivision would then flip to 2, then we fill in all the divisions subdivision 2.

i don't think the way it's currently programmed is efficient and there should be some other way to do this.

any thoughts or help on this would be great.

like image 365
Micah H. Avatar asked Jan 30 '26 18:01

Micah H.


2 Answers

I think you can get rid of the chain of ifs by replacing with

long empiresubID = (myMaxId / 1600) + 1;
like image 118
Sergey Kalinichenko Avatar answered Feb 02 '26 08:02

Sergey Kalinichenko


Your method can be replaced by two lines:

long divisionID = myMaxId % 40 == 0 ? 1 : myMaxId % 40;
long empiresubID = (myMaxId / 1600) + 1;
like image 40
Bohemian Avatar answered Feb 02 '26 09:02

Bohemian