MsCrelix MsCrelix - 1 month ago 8
Android Question

Android Method Code too Large, What to Do?

guys. Got problem with one of my method codes, it's to big. Code compiles, but when that method need to be used, app closes and shows error. I don't want to rewrite my code. Wanted to seperate it, but same problem.

Made another method and made call in main method, but same.

}
checkpacks();
}

public void checkpacks(){
//Code


I know my Code are large, and it's still not completed becouse of this problem, code will be more larger..

Code:

public void getBoxes(View view) {
final Context context = this;

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);

alertDialogBuilder.setTitle("Congratulations");

alertDialogBuilder
.setMessage("Message")
.setCancelable(false)
.setPositiveButton(getString(R.string.nice), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {

}
});

AlertDialog BoxDialog = alertDialogBuilder.create();

CarMain.main[2] += 1;
CarMain.main[3] += 1;
if (CarMain.main[2] == 5) {
CarMain.main[2] -= 5;
int PlayerLevel = CarMain.main[5];
switch (PlayerLevel) {
case 1:
Pack.packs[0] = +1;
break;
case 2:
Random PlayerLv2Generator = new Random();
int PlayerLv2Int = PlayerLv2Generator.nextInt(2) + 1;
switch (PlayerLv2Int) {
case 1:
Pack.packs[0] = +1;
break;
case 2:
Pack.packs[2] = +1;
break;
}
break;
case 3:
Random PlayerLv3Generator = new Random();
int PlayerLv3Int = PlayerLv3Generator.nextInt(3) + 1;
switch (PlayerLv3Int) {
case 1:
Pack.packs[0] = +1;
break;
case 2:
Pack.packs[2] = +1;
break;
case 3:
Pack.packs[4] = +1;
break;
}
break;
case 4:
Random PlayerLv4Generator = new Random();
int PlayerLv4Int = PlayerLv4Generator.nextInt(4) + 1;
switch (PlayerLv4Int) {
case 1:
Pack.packs[0] = +1;
break;
case 2:
Pack.packs[2] = +1;
break;
case 3:
Pack.packs[4] = +1;
break;
case 4:
Pack.packs[6] = +1;
break;
}
break;
case 5:
Random PlayerLv5Generator = new Random();
int PlayerLv5Int = PlayerLv5Generator.nextInt(5) + 1;
switch (PlayerLv5Int) {
case 1:
Pack.packs[0] = +1;
break;
case 2:
Pack.packs[2] = +1;
break;
case 3:
Pack.packs[4] = +1;
break;
case 4:
Pack.packs[6] = +1;
break;
case 5:
Pack.packs[8] = +1;
break;
}
break;
case 6:
Random PlayerLv6Generator = new Random();
int PlayerLv6Int = PlayerLv6Generator.nextInt(6) + 1;
switch (PlayerLv6Int) {
case 1:
Pack.packs[0] = +1;
break;
case 2:
Pack.packs[2] = +1;
break;
case 3:
Pack.packs[4] = +1;
break;
case 4:
Pack.packs[6] = +1;
break;
case 5:
Pack.packs[8] = +1;
break;
case 6:
Pack.packs[10] = +1;
break;
}
break;
case 7:
Random PlayerLv7Generator = new Random();
int PlayerLv7Int = PlayerLv7Generator.nextInt(7) + 1;
switch (PlayerLv7Int) {
case 1:
Pack.packs[0] = +1;
break;
case 2:
Pack.packs[2] = +1;
break;
case 3:
Pack.packs[4] = +1;
break;
case 4:
Pack.packs[6] = +1;
break;
case 5:
Pack.packs[8] = +1;
break;
case 6:
Pack.packs[10] = +1;
break;
case 7:
Pack.packs[12] = +1;
break;
}
break;
case 8:
Random PlayerLv8Generator = new Random();
int PlayerLv8Int = PlayerLv8Generator.nextInt(8) + 1;
switch (PlayerLv8Int) {
case 1:
Pack.packs[0] = +1;
break;
case 2:
Pack.packs[2] = +1;
break;
case 3:
Pack.packs[4] = +1;
break;
case 4:
Pack.packs[6] = +1;
break;
case 5:
Pack.packs[8] = +1;
break;
case 6:
Pack.packs[10] = +1;
break;
case 7:
Pack.packs[12] = +1;
break;
case 8:
Pack.packs[14] = +1;
break;
}
break;
case 9:
Random PlayerLv9Generator = new Random();
int PlayerLv9Int = PlayerLv9Generator.nextInt(9) + 1;
switch (PlayerLv9Int) {
case 1:
Pack.packs[0] = +1;
break;
case 2:
Pack.packs[2] = +1;
break;
case 3:
Pack.packs[4] = +1;
break;
case 4:
Pack.packs[6] = +1;
break;
case 5:
Pack.packs[8] = +1;
break;
case 6:
Pack.packs[10] = +1;
break;
case 7:
Pack.packs[12] = +1;
break;
case 8:
Pack.packs[14] = +1;
break;
case 9:
Pack.packs[16] = +1;
break;
}
break;
case 10:
Random PlayerLv10Generator = new Random();
int PlayerLv10Int = PlayerLv10Generator.nextInt(10) + 1;
switch (PlayerLv10Int) {
case 1:
Pack.packs[0] = +1;
break;
case 2:
Pack.packs[2] = +1;
break;
case 3:
Pack.packs[4] = +1;
break;
case 4:
Pack.packs[6] = +1;
break;
case 5:
Pack.packs[8] = +1;
break;
case 6:
Pack.packs[10] = +1;
break;
case 7:
Pack.packs[12] = +1;
break;
case 8:
Pack.packs[14] = +1;
break;
case 9:
Pack.packs[16] = +1;
break;
case 10:
Pack.packs[18] = +1;
break;
}
}
}
if (Pack.packs[0] == 1) {
Pack.packs[0] -= 1;
Random PackLv1Generator = new Random();
int PackLv1Int = PackLv1Generator.nextInt(12) + 1;
switch (PackLv1Int) {
case 1:
Box.boxes[0] += 1; // Accessories Pack 1
BoxDialog.setMessage("You have got " + getString(R.string.Accessories_Pack_1));
BoxDialog.show();
break;
case 2:
Box.boxes[40] += 1; // Body Pack 1
BoxDialog.setMessage("You have got " + getString(R.string.Body_Pack_1));
BoxDialog.show();
break;
case 3:
Box.boxes[80] += 1; // Brakes Pack 1
BoxDialog.setMessage("You have got " + getString(R.string.Brakes_Pack_1));
BoxDialog.show();
break;
case 4:
Box.boxes[120] += 1; // Cooling Pack 1
BoxDialog.setMessage("You have got " + getString(R.string.Cooling_Pack_1));
BoxDialog.show();
break;
case 5:
Box.boxes[160] += 1; // Electronics Pack 1
BoxDialog.setMessage("You have got " + getString(R.string.Electronics_Pack_1));
BoxDialog.show();
break;
case 6:
Box.boxes[200] += 1; // Engine Pack 1
BoxDialog.setMessage("You have got " + getString(R.string.Engine_Pack_1));
BoxDialog.show();
break;
case 7:
Box.boxes[240] += 1; // Exhaust Pack 1
BoxDialog.setMessage("You have got " + getString(R.string.Exhaust_Pack_1));
BoxDialog.show();
break;
case 8:
Box.boxes[280] += 1; // Fuel Pack 1
BoxDialog.setMessage("You have got " + getString(R.string.Fuel_Pack_1));
BoxDialog.show();
break;
case 9:
Box.boxes[320] += 1; // Steering Pack 1
BoxDialog.setMessage("You have got " + getString(R.string.Steering_Pack_1));
BoxDialog.show();
break;
case 10:
Box.boxes[360] += 1; // Suspension Pack 1
BoxDialog.setMessage("You have got " + getString(R.string.Suspension_Pack_1));
BoxDialog.show();
break;
case 11:
Box.boxes[400] += 1; // Transmission Pack 1
BoxDialog.setMessage("You have got " + getString(R.string.Transmission_Pack_1));
BoxDialog.show();
break;
case 12:
Box.boxes[440] += 1; // Tires Pack 1
BoxDialog.setMessage("You have got " + getString(R.string.Tires_Pack_1));
BoxDialog.show();
break;
}
}
if (Pack.packs[2] == 1) {
Pack.packs[2] -= 1;
Random PackLv2Generator = new Random();
int PackLv2Int = PackLv2Generator.nextInt(12) + 1;
switch (PackLv2Int) {
case 1:
Box.boxes[4] += 1; // Accessories Pack 2
BoxDialog.setMessage("You have got " + getString(R.string.Accessories_Pack_2));
BoxDialog.show();
break;
case 2:
Box.boxes[44] += 1; // Body Pack 2
BoxDialog.setMessage("You have got " + getString(R.string.Body_Pack_2));
BoxDialog.show();
break;
case 3:
Box.boxes[84] += 1; // Brakes Pack 2
break;
case 4:
Box.boxes[124] += 1; // Cooling Pack 2
break;
case 5:
Box.boxes[164] += 1; // Electronics Pack 2
break;
case 6:
Box.boxes[204] += 1; // Engine Pack 2
break;
case 7:
Box.boxes[244] += 1; // Exhaust Pack 2
break;
case 8:
Box.boxes[284] += 1; // Fuel Pack 2
break;
case 9:
Box.boxes[324] += 1; // Steering Pack 2
break;
case 10:
Box.boxes[364] += 1; // Suspension Pack 2
break;
case 11:
Box.boxes[404] += 1; // Transmission Pack 2
break;
case 12:
Box.boxes[444] += 1; // Tires Pack 2
break;
}
}
if (Pack.packs[4] == 1) {
Pack.packs[4] -= 1;
Random PackLv3Generator = new Random();
int PackLv3Int = PackLv3Generator.nextInt(12) + 1;
switch (PackLv3Int) {
case 1:
Box.boxes[8] += 1; // Accessories Pack 3
BoxDialog.setMessage("You have got " + getString(R.string.Accessories_Pack_3));
BoxDialog.show();
break;
case 2:
Box.boxes[48] += 1; // Body Pack 3
BoxDialog.setMessage("You have got " + getString(R.string.Body_Pack_3));
BoxDialog.show();
break;
case 3:
Box.boxes[88] += 1; // Brakes Pack 3
break;
case 4:
Box.boxes[128] += 1; // Cooling Pack 3
break;
case 5:
Box.boxes[168] += 1; // Electronics Pack 3
break;
case 6:
Box.boxes[208] += 1; // Engine Pack 3
break;
case 7:
Box.boxes[248] += 1; // Exhaust Pack 3
break;
case 8:
Box.boxes[288] += 1; // Fuel Pack 3
break;
case 9:
Box.boxes[328] += 1; // Steering Pack 3
break;
case 10:
Box.boxes[368] += 1; // Suspension Pack 3
break;
case 11:
Box.boxes[408] += 1; // Transmission Pack 3
break;
case 12:
Box.boxes[448] += 1; // Tires Pack 3
break;
}
}
if (Pack.packs[6] == 1) {
Pack.packs[6] -= 1;
Random PackLv4Generator = new Random();
int PackLv4Int = PackLv4Generator.nextInt(12) + 1;
switch (PackLv4Int) {
case 1:
Box.boxes[12] += 1; // Accessories Pack 4
BoxDialog.setMessage("You have got " + getString(R.string.Accessories_Pack_4));
BoxDialog.show();
break;
case 2:
Box.boxes[52] += 1; // Body Pack 4
BoxDialog.setMessage("You have got " + getString(R.string.Body_Pack_4));
BoxDialog.show();
break;
case 3:
Box.boxes[92] += 1; // Brakes Pack 4
break;
case 4:
Box.boxes[132] += 1; // Cooling Pack 4
break;
case 5:
Box.boxes[172] += 1; // Electronics Pack 4
break;
case 6:
Box.boxes[212] += 1; // Engine Pack 4
break;
case 7:
Box.boxes[252] += 1; // Exhaust Pack 4
break;
case 8:
Box.boxes[292] += 1; // Fuel Pack 4
break;
case 9:
Box.boxes[332] += 1; // Steering Pack 4
break;
case 10:
Box.boxes[372] += 1; // Suspension Pack 4
break;
case 11:
Box.boxes[412] += 1; // Transmission Pack 4
break;
case 12:
Box.boxes[452] += 1; // Tires Pack 4
break;
}
}
if (Pack.packs[8] == 1) {
Pack.packs[8] -= 1;
Random PackLv5Generator = new Random();
int PackLv5Int = PackLv5Generator.nextInt(12) + 1;
switch (PackLv5Int) {
case 1:
Box.boxes[16] += 1; // Accessories Pack 5
BoxDialog.setMessage("You have got " + getString(R.string.Accessories_Pack_5));
BoxDialog.show();
break;
case 2:
Box.boxes[56] += 1; // Body Pack 5
BoxDialog.setMessage("You have got " + getString(R.string.Body_Pack_5));
BoxDialog.show();
break;
case 3:
Box.boxes[96] += 1; // Brakes Pack 5
break;
case 4:
Box.boxes[136] += 1; // Cooling Pack 5
break;
case 5:
Box.boxes[176] += 1; // Electronics Pack 5
break;
case 6:
Box.boxes[216] += 1; // Engine Pack 5
break;
case 7:
Box.boxes[256] += 1; // Exhaust Pack 5
break;
case 8:
Box.boxes[296] += 1; // Fuel Pack 5
break;
case 9:
Box.boxes[336] += 1; // Steering Pack 5
break;
case 10:
Box.boxes[376] += 1; // Suspension Pack 5
break;
case 11:
Box.boxes[416] += 1; // Transmission Pack 5
break;
case 12:
Box.boxes[456] += 1; // Tires Pack 5
break;
}
}
if (Pack.packs[10] == 1) {
Pack.packs[10] -= 1;
Random PackLv6Generator = new Random();
int PackLv6Int = PackLv6Generator.nextInt(12) + 1;
switch (PackLv6Int) {
case 1:
Box.boxes[20] += 1; // Accessories Pack 6
BoxDialog.setMessage("You have got " + getString(R.string.Accessories_Pack_6));
BoxDialog.show();
break;
case 2:
Box.boxes[60] += 1; // Body Pack 6
BoxDialog.setMessage("You have got " + getString(R.string.Body_Pack_6));
BoxDialog.show();
break;
case 3:
Box.boxes[100] += 1; // Brakes Pack 6
break;
case 4:
Box.boxes[140] += 1; // Cooling Pack 6
break;
case 5:
Box.boxes[180] += 1; // Electronics Pack 6
break;
case 6:
Box.boxes[220] += 1; // Engine Pack 6
break;
case 7:
Box.boxes[260] += 1; // Exhaust Pack 6
break;
case 8:
Box.boxes[300] += 1; // Fuel Pack 6
break;
case 9:
Box.boxes[340] += 1; // Steering Pack 6
break;
case 10:
Box.boxes[380] += 1; // Suspension Pack 6
break;
case 11:
Box.boxes[420] += 1; // Transmission Pack 6
break;
case 12:
Box.boxes[460] += 1; // Tires Pack 6
break;
}
}
if (Pack.packs[12] == 1) {
Pack.packs[12] -= 1;
Random PackLv7Generator = new Random();
int PackLv7Int = PackLv7Generator.nextInt(12) + 1;
switch (PackLv7Int) {
case 1:
Box.boxes[24] += 1; // Accessories Pack 7
BoxDialog.setMessage("You have got " + getString(R.string.Accessories_Pack_7));
BoxDialog.show();
break;
case 2:
Box.boxes[64] += 1; // Body Pack 7
BoxDialog.setMessage("You have got " + getString(R.string.Body_Pack_7));
BoxDialog.show();
break;
case 3:
Box.boxes[104] += 1; // Brakes Pack 7
break;
case 4:
Box.boxes[144] += 1; // Cooling Pack 7
break;
case 5:
Box.boxes[184] += 1; // Electronics Pack 7
break;
case 6:
Box.boxes[224] += 1; // Engine Pack 7
break;
case 7:
Box.boxes[264] += 1; // Exhaust Pack 7
break;
case 8:
Box.boxes[304] += 1; // Fuel Pack 7
break;
case 9:
Box.boxes[344] += 1; // Steering Pack 7
break;
case 10:
Box.boxes[384] += 1; // Suspension Pack 7
break;
case 11:
Box.boxes[424] += 1; // Transmission Pack 7
break;
case 12:
Box.boxes[464] += 1; // Tires Pack 7
break;
}
}
if (Pack.packs[14] == 1) {
Pack.packs[14] -= 1;
Random PackLv8Generator = new Random();
int PackLv8Int = PackLv8Generator.nextInt(12) + 1;
switch (PackLv8Int) {
case 1:
Box.boxes[28] += 1; // Accessories Pack 8
BoxDialog.setMessage("You have got " + getString(R.string.Accessories_Pack_8));
BoxDialog.show();
break;
case 2:
Box.boxes[68] += 1; // Body Pack 8
BoxDialog.setMessage("You have got " + getString(R.string.Body_Pack_8));
BoxDialog.show();
break;
case 3:
Box.boxes[108] += 1; // Brakes Pack 8
break;
case 4:
Box.boxes[148] += 1; // Cooling Pack 8
break;
case 5:
Box.boxes[188] += 1; // Electronics Pack 8
break;
case 6:
Box.boxes[228] += 1; // Engine Pack 8
break;
case 7:
Box.boxes[268] += 1; // Exhaust Pack 8
break;
case 8:
Box.boxes[308] += 1; // Fuel Pack 8
break;
case 9:
Box.boxes[348] += 1; // Steering Pack 8
break;
case 10:
Box.boxes[388] += 1; // Suspension Pack 8
break;
case 11:
Box.boxes[428] += 1; // Transmission Pack 8
break;
case 12:
Box.boxes[468] += 1; // Tires Pack 8
break;
}
}
if (Pack.packs[16] == 1) {
Pack.packs[16] -= 1;
Random PackLv9Generator = new Random();
int PackLv9Int = PackLv9Generator.nextInt(12) + 1;
switch (PackLv9Int) {
case 1:
Box.boxes[32] += 1; // Accessories Pack 9
BoxDialog.setMessage("You have got " + getString(R.string.Accessories_Pack_9));
BoxDialog.show();
break;
case 2:
Box.boxes[72] += 1; // Body Pack 9
BoxDialog.setMessage("You have got " + getString(R.string.Body_Pack_9));
BoxDialog.show();
break;
case 3:
Box.boxes[112] += 1; // Brakes Pack 9
break;
case 4:
Box.boxes[152] += 1; // Cooling Pack 9
break;
case 5:
Box.boxes[192] += 1; // Electronics Pack 9
break;
case 6:
Box.boxes[232] += 1; // Engine Pack 9
break;
case 7:
Box.boxes[272] += 1; // Exhaust Pack 9
break;
case 8:
Box.boxes[312] += 1; // Fuel Pack 9
break;
case 9:
Box.boxes[352] += 1; // Steering Pack 9
break;
case 10:
Box.boxes[392] += 1; // Suspension Pack 9
break;
case 11:
Box.boxes[432] += 1; // Transmission Pack 9
break;
case 12:
Box.boxes[472] += 1; // Tires Pack 9
break;
}
}
}
}

Answer

Here is an example of how you can reduce the size of your code:

Your switch (PlayerLevel) { can be reduced to:

Random gen = new Random();
int rndValue = gen.nextInt(PlayerLevel) + 1;
switch (rndValue) {
    case 1:
        Pack.packs[0] = +1;
        break;
    case 2:
        Pack.packs[2] = +1;
        break;
    case 3:
        Pack.packs[4] = +1;
        break;
    case 4:
        Pack.packs[6] = +1;
        break;
    case 5:
        Pack.packs[8] = +1;
        break;
    case 6:
        Pack.packs[10] = +1;
        break;
    case 7:
        Pack.packs[12] = +1;
        break;
    case 8:
        Pack.packs[14] = +1;
        break;
    case 9:
        Pack.packs[16] = +1;
        break;
    case 10:
        Pack.packs[18] = +1;
        break;
}

Which as you can see, can be replaced by

Random gen = new Random();
int rndValue = gen.nextInt(PlayerLevel);
Pack.packs[rndValue / 2] += 1;

This covers the whole 10 levels, because of how the random is generated. (What do you put in the odd indices, by the way?)

Then, you really only need one Random. You can create it once and use it everywhere, no need to re-create it every time you need one.

Also, please use objects and properties instead of big arrays with comments.

Consider changing your model, and replacing for example

Box.boxes[280] += 1; // Fuel Pack 1

with

Box.fuelPack[0] += 1;

(Quick question, why are your indices all exactly 4 apart? except for packs where it is 2?)

Edit

It looks like you are incrementing packs[n] only to decrement it right after. May be you can just use a variable instead:

Random gen = new Random();
int pack = gen.nextInt(PlayerLevel);
// Then switch on that value.

Then, all packs results result in something like int PackLv3Int = PackLv3Generator.nextInt(12) + 1;

You can do that only once: int packLvl = gen.nextInt(12);

Then what you are really incrementing is

Box.boxes[pack * 4 + packLvl * 40];

And if you are always going to call BoxDialog.show();, call it once at the end.

more edits

Here is how you can re-write your method in a few lines:

String[] labels = new String {"Accessories", "Body", "Brakes", "Cooling", "Electronics", "Engine", "Exhaust", "Fuel", "Steering", "Suspensions", "Transmission", "Tires"};
Random gen = new Random();

int pack = gen.nextInt(PlayerLevel);
int packLvl = gen.nextInt(labels.length);

Box.boxes[pack * 4 + packLvl * 40];

String packageName = getPackageName();
int resId = getResources().getIdentifier(aString, labels[pack] + "_Pack_" + (packLvl + 1), packageName);
BoxDialog.setMessage("You have got " + getString(resId));
BoxDialog.show();