# Thread: How can I shorten this?

1. Member
Join Date
Dec 2016
Posts
2
Rep Power
0

## How can I shorten this?

How can i shorten the number of lines of code? This is to calculate the amount spent each day for the 12 days of christmas using NetBeans. This is the best I could do and dont know how to shorten/simplify it from here

Java Code:
```public static void main(String[] args) {
int[] costsarr = {210, 375, 181, 600, 750, 360, 13125, 58, 7553, 5509, 2708, 2934};
System.out.println("Cost per day");
for (int y = 0; y < costsarr.length; y++) {

int sum = 0;

if (y == 0) {

sum = sum + costsarr[0];

System.out.println("Day " + (y + 1) + ": \$" + sum);
}

if (y == 1) {

sum = sum + costsarr[0] + costsarr[1] * 2;

System.out.println("Day " + (y + 1) + ": \$" + sum);
}

if (y == 2) {

sum = sum + costsarr[0] + costsarr[1] * 2 + costsarr[2] * 3;

System.out.println("Day " + (y + 1) + ": \$" + sum);
}

if (y == 3) {

sum = sum + costsarr[0] + costsarr[1] * 2 + costsarr[2] * 3 + costsarr[3] * 4;

System.out.println("Day " + (y + 1) + ": \$" + sum);
}

if (y == 4) {

sum = sum + costsarr[0] + costsarr[1] * 2 + costsarr[2] * 3 + costsarr[3] * 4 + costsarr[4] * 5;

System.out.println("Day " + (y + 1) + ": \$" + sum);
}
if (y == 5) {

sum = sum + costsarr[0] + costsarr[1] * 2 + costsarr[2] * 3 + costsarr[3] * 4 + costsarr[4] * 5 + costsarr[5] * 6;

System.out.println("Day " + (y + 1) + ": \$" + sum);
}
if (y == 6) {

sum = sum + costsarr[0] + costsarr[1] * 2 + costsarr[2] * 3 + costsarr[3] * 4 + costsarr[4] * 5 + costsarr[5] * 6 + costsarr[6] * 7;

System.out.println("Day " + (y + 1) + ": \$" + sum);
}
if (y == 7) {

sum = sum + costsarr[0] + costsarr[1] * 2 + costsarr[2] * 3 + costsarr[3] * 4 + costsarr[4] * 5 + costsarr[5] * 6 + costsarr[6] * 7 + costsarr[7] * 8;

System.out.println("Day " + (y + 1) + ": \$" + sum);
}
if (y == 8) {

sum = sum + costsarr[0] + costsarr[1] * 2 + costsarr[2] * 3 + costsarr[3] * 4 + costsarr[4] * 5 + costsarr[5] * 6 + costsarr[6] * 7 + costsarr[7] * 8 + costsarr[8] * 9;

System.out.println("Day " + (y + 1) + ": \$" + sum);
}
if (y == 9) {

sum = sum + costsarr[0] + costsarr[1] * 2 + costsarr[2] * 3 + costsarr[3] * 4 + costsarr[4] * 5 + costsarr[5] * 6 + costsarr[6] * 7 + costsarr[7] * 8 + costsarr[8] * 9 + costsarr[9] * 10;

System.out.println("Day " + (y + 1) + ": \$" + sum);
}
if (y == 10) {

sum = sum + costsarr[0] + costsarr[1] * 2 + costsarr[2] * 3 + costsarr[3] * 4 + costsarr[4] * 5 + costsarr[5] * 6 + costsarr[6] * 7 + costsarr[7] * 8 + costsarr[8] * 9 + costsarr[9] * 10 + costsarr[10] * 11;

System.out.println("Day " + (y + 1) + ": \$" + sum);
}
if (y == 11) {

sum = sum + costsarr[0] + costsarr[1] * 2 + costsarr[2] * 3 + costsarr[3] * 4 + costsarr[4] * 5 + costsarr[5] * 6 + costsarr[6] * 7 + costsarr[7] * 8 + costsarr[8] * 9 + costsarr[9] * 10 + costsarr[10] * 11 + costsarr[11] * 12;

System.out.println("Day " + (y + 1) + ": \$" + sum);
}
if (y == 12) {

sum = sum + costsarr[0] + costsarr[1] * 2 + costsarr[2] * 3 + costsarr[3] * 4 + costsarr[4] * 5 + costsarr[5] * 6 + costsarr[6] * 7 + costsarr[7] * 8 + costsarr[8] * 9 + costsarr[9] * 10 + costsarr[10] * 11 + costsarr[11] * 12 + costsarr[12] * 13;

System.out.println("Day " + (y + 1) + ": \$" + sum);
}
}}}```
Last edited by deussoj; 12-08-2016 at 12:42 AM.

2. ## Re: How can I shorten this?

[code]
[/code]

to get highlighting and preserve formatting.

Be sure the code is properly indented. Code without indentations is harder to read and understand.

Why have a loop? Just have the statements one after the other. Get rid of the if statements with the y == tests
Last edited by Norm; 12-08-2016 at 12:34 AM.

3. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
13

## Re: How can I shorten this?

Java Code:
```      int sum = costsarr[0];
System.out.println("Day 1: \$" + sum);
for (int y = 1; y < 12; y++) {
sum += costsarr[y] + costsarr[y]*y;
System.out.println("Day " + (y+1) + ": \$" + sum);
}```
You could eliminate line 2 and init sum to 0 and put the whole thing in a loop. But then you do
a useless multiply by 0 which doesn't sit well with me.

Regards,
Jim

4. Member
Join Date
Dec 2016
Posts
2
Rep Power
0

## Re: How can I shorten this?

Originally Posted by jim829
Java Code:
```      int sum = costsarr[0];
System.out.println("Day 1: \$" + sum);
for (int y = 1; y < 12; y++) {
sum += costsarr[y] + costsarr[y]*y;
System.out.println("Day " + (y+1) + ": \$" + sum);
}```
You could eliminate line 2 and init sum to 0 and put the whole thing in a loop. But then you do
a useless multiply by 0 which doesn't sit well with me.

Regards,
Jim
Thank you so much! this helped me a lot and now I understand.