0

My coding is below beginner at this stage and I need help, My lecturer asked me to " use a loop to calculate the total of all annual rents in the array" which means that the user enters what the rent would be over 12 months, How is it that I would make a loop to calculate the Sum of numbers that are not even entered in the code and are going through an array, I will put the code for the array in here and any help would be appreciated.

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();
}
for (int i = 0; i < array.length; i++) {
  System.out.println(array[i]);
}
13
  • 1
    your requirement is not clear. Commented Jun 27, 2014 at 11:26
  • 1
    How would you do it on paper? (Hint: Do it on paper one row at a time, vs using the technique of adding all the numbers at once.) Commented Jun 27, 2014 at 11:26
  • I need to make a Loop, that would calculate the total of an array that does not have predefined definitions. Commented Jun 27, 2014 at 11:28
  • You have a loop. Do something in it. Commented Jun 27, 2014 at 11:29
  • 2
    @TAsk If he wants to make a living out of this, he better starts appreciating coffee. Commented Jun 27, 2014 at 11:34

1 Answer 1

3

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];
}
Sign up to request clarification or add additional context in comments.

6 Comments

:/ It wasn't supposed to be answered FOR me, And even so when I enter that a new error pops up, Im missing a return statement somewhere.. My head is wrecked from this x)
@Joetjah I need to declare the totalSum In another case of a switch statement, is this possible with the coding provided? I tried this code System.out.println("The annual rent for propery code "+propertyCode+" is: " + totalSum);
@JakeGrim It seems like your code, the one you've posted in the question, is situated in a method. In this case, your method does want something to return. Read this for more clarity: homeandlearn.co.uk/java/java_methods.html
@JakeGrim There is no switch-statement here. And I don't know where propertyCode comes from.
Thanks :) I fixed it! I had to move the property code and the totalSum constructors to the main method so that It could be accessed correctly
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.