Let's take a look at your current code:
Scanner keyboardScanner = new Scanner(System.in);
This essentially loads in a Scanner-object, which in your case is being used to read input given from the user.
double[] array = new double[12];
You want to save numeric values for every month in a year. A year has 12 months, hence an array of doubles is created. This is a list of numerics (with size 12), so we can save the value for each month in here.
for (int i = 0; i < 12; i++) {
So, before we can do anything, we need to know the input of the user. The user needs to insert a value 12 times. Hence, we ask the user 12 times to insert a value, then save that value in our created array.
The perfect way to achieve this is by using a for-loop. Essentially, what your loop does, is the following: first, we create a counter to tick through your loop, and we name it i. We set it to 0. Next, we tell the loop when to stop looping. In this case, it will stop if i is the same or higher then 12. Next, we tell the loop what to do after each loop. In this case, we increase the value with 1 by using i++.
System.out.println("Enter Rental for month[" + i + "]");
array[i] = keyboardScanner.nextDouble();
}
The value the user inserts, needs to be saved. So each time the user inserts a value, we can collect that value by using keyboardScanner.nextDouble(). The array you've created has 12 places: from array[0] to array[11]. Since we let i start on 0 and end on 11, we can set the value in the array on the i with the value inserted. So, the value you inserted the 3th time (i=2) gets saved on array[2].
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
Now, we are going to do the same again. For the complete length of array, we are outputting the value in that array. This will result in:
System.out.println(array[0]);
System.out.println(array[1]);
System.out.println(array[2]);
...
Now, think of what you can do with that array. You're printing every number already in System.out.println(array[i]);. So, your value array contains all the numbers.
You should create a new double variable in which you can store the total value. And then, add all the values from the array to that double.
So, we are going to edit your original code:
Scanner keyboardScanner = new Scanner(System.in);
double[] array = new double[12];
for (int i = 0; i < 12; i++) {
System.out.println("Enter Rental for month[" + i + "]");
array[i] = keyboardScanner.nextDouble();
}
//Declaring the variable totalSum, which holds the total sum of all values in the array.
double totalSum = 0;
//So now, we need to do something with that array and sum up all the values in that array.
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
//Outputting the result of the calculation
System.out.println("The total value is: " + totalSum);
Allright, let's put that in a textual way:
- I declare a variable named totalSum. I set that value to 0.
- For each value in the array, meaning for each month, I like to add up that amount to the totalscore totalSum.
- And now I will output that totalSum, so the user sees the sum of the months.
So, as a final touch, change it to the following:
//So now, we need to take each value and add them up to the totalSum
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
totalSum = totalSum + array[i];
}