1

My problem is based on the compound interest formula. A = P(1 + r)^n. I have a number of values for r and n which I must store as an array. I also must output my range of final values A as an array also. I think I have stored r and n correctly as an array. However my problem is with the final values of A and storing each value A. SO far this is what I have written.

import java.util.Scanner;
import javax.swing.*;

public class Prin {
public static void main (String [] args){

    System.out.println("Please enter the Principal you wish to invest =>");
    Scanner stdio = new Scanner (System.in);
    int principal = stdio.nextInt(); 
    stdio.nextLine();

    System.out.println("This program will now calculate the final amount A for (years) n = 1,2,3,4 and the rate r = 1%, 1.5%, 2%, 2.5%, 3%");

    int yearsarray[] = {1,2,3,4};
    double ratearray[] = {0.010, 0.015, 0.020, 0.025, 0.030};

    double amountarray[];
    amountarray = new double[19];


    for(int i=0; i<=3; i++)
    {
        for(int j=0; j<=5; j++){

            amountarray[k] = principal * Math.pow((1 + ratearray[j]), yearsarray[i]);

            System.out.println(" answer " + amountarray[k] );


    }
}

Do I need another for loop to increase the values of k in amountarray[]?

I would like to have all values of amountarray i.e amountarray[0], amountarray[1], amountarray[2], .......and so on.

Thanks in advance for any help.

Nargis
  • 4,687
  • 1
  • 28
  • 45

4 Answers4

3
amountarray = new double[19];

The code above is false because you need to have 4x5 = 20 double values

This code will always work properly, You should use this and you need to learn write your codes like this:

public class Prin {
public static void main (String [] args){

    //...
    int[] yearsarray = {1,2,3,4};
    double[] ratearray = {0.010, 0.015, 0.020, 0.025, 0.030};
    double[] amountarray = new double[yearsarray.length * ratearray.length];

    int k = 0;

    for(int i=0; i<yearsarray.length; i++){
        for(int j=0; j<ratearray.length; j++){
            amountarray[k] = principal * Math.pow((1 + ratearray[j]), yearsarray[i]);
            System.out.println(" answer " + amountarray[k] );
            k++;
    }
}
SpongeBobFan
  • 964
  • 5
  • 13
  • I corrected my code. In you code, the programmer will get an ArrayIndexOutOfBoundsException. 'amountarray = new double[19];' –  Aug 20 '13 at 11:55
  • indeed, I took over that part verbatim from OP. As I felt there were several minor issues I rewrote the whole loop thing to propose improvements for all those that I spotted, have a look at the new version/ – fvu Aug 20 '13 at 12:05
2

"This program will now calculate the final amount A for (years) n = 1,2,3,4 and the rate r = 1%, 1.5%, 2%, 2.5%, 3%"

... this implies your answer comes in the form of a 2-dimensional matrix.

Hence your amountarray needs to be defined as:

double amountarray[][] = new double[yearsarray.length][ratearray.length];

Then you would calculate:

amountarray[i][j] = principal * Math.pow((1 + ratearray[j]), yearsarray[i]);
Stewart
  • 17,616
  • 8
  • 52
  • 80
  • 1
    That does make much more sense. The specification on my question was for 2 individual arrays. I much prefer your way. – user2699666 Aug 21 '13 at 11:38
1

No, you don't

int k=0; // initialization
for(int i=0; i<=3; i++)
{
    for(int j=0; j<=5; j++){
        // use post-increment
        amountarray[k] = principal * Math.pow((1 + ratearray[j]), yearsarray[i]);
        System.out.println(" answer " + amountarray[k] );
        // now we can increment k
        k = k+1;
}

Also this: as you seem to be using yearsarray just to get a value that's i+1, why just not do

amountarray[k] = principal * Math.pow((1 + ratearray[j]), i+1);

That way you can get rid of yearsarray, at least in this case

EDIT: a reworked version that also handles a couple of other minor issues and reduced usage of "magic numbers"

public class Prin {

    public static void main(String[] args) {

        System.out.println("Please enter the Principal you wish to invest =>");
        Scanner stdio = new Scanner(System.in);
        int principal = stdio.nextInt();
        stdio.nextLine();

        System.out.println("This program will now calculate the final amount A for (years) n = 1,2,3,4 and the rate r = 1%, 1.5%, 2%, 2.5%, 3%");

        int yearsarray[] = {1, 2, 3, 4};
        double ratearray[] = {0.010, 0.015, 0.020, 0.025, 0.030};

        double amountarray[];
        // this way the array will follow the size of yearsarray and ratearray
        amountarray = new double[yearsarray.length * ratearray.length];


        int k = 0; // initialization
        for (int i = 0; i <= yearsarray.length; i++) {
            System.out.println("years=" + yearsarray[i]);
            for (int j = 0; j < ratearray.length; j++) {
                // use post-increment
                amountarray[k] = principal * Math.pow((1 + ratearray[j]), yearsarray[i]);
                System.out.println("  " + ratearray[j] + " answer " + amountarray[k]);
                k+=1;
            }
        }
    }
}
fvu
  • 32,488
  • 6
  • 61
  • 79
  • Variable increment `k++` hidden inside the `System.out.println` line? Ouch. – Stewart Aug 20 '13 at 15:23
  • 1
    @Stewart yeah I agree, blame it on a series of small edits. And that's also why I offered the postincrement-less version. But you are 100% right, and that's why I removed all references to postincrements. – fvu Aug 20 '13 at 15:47
0

Try to store your data in a Map, this is an example of your loop

 for(int i=0; i<=yearsarray.length; i++) {
     for(int j=0; j<=ratearray.length; j++) {
         double answer = (yearsarray, principal * Math.pow((1 + ratearray[j]), yearsarray[i]));
         Collection<Double> aux;
         if (!answers.containsKey(yearsarray[i])){
             aux = new ArrayList<Double>();         
             aux.add(answer);
         } else {
             aux = answers.get(yearsarray[i]);
             aux.add(answer);
         }
         answers.put(yearsarray[i], aux);
         // include the answers in a Map 
    }
}

Thanks

Stewart
  • 17,616
  • 8
  • 52
  • 80
Felquir
  • 431
  • 1
  • 4
  • 12
  • I am not familiar with maps but it is something I will research. Thanks. – user2699666 Aug 21 '13 at 11:40
  • you shouldn't use arrays their solution is valid but isn't correct, http://stackoverflow.com/questions/1589813/when-to-use-a-list-over-an-array-in-java – Felquir Aug 21 '13 at 12:39