-2

How can I make the result for this loop an array of the differents results:

for (int i = 1; i <= years; i++) {
    double newbalance = account.getBalance() * rate;
    account.deposit(newbalance);
    String test = String.valueOf(account.getBalance()) + "\n";
    result.append(test);
}

For example:

If the user put years 10, that give me 10 results.

I need:

  1. To put these 10 results in an array.
  2. If the user eraser that input, an then press a specific button (I already have the code), the data need to be deleted and calculate with the new values.

Thank you.

Koby Douek
  • 16,156
  • 19
  • 74
  • 103
Camila
  • 1
  • 3

1 Answers1

0
String[] result = new String[years];

for (int i = 1; i <= years; i++) {
    double newbalance = account.getBalance() * rate;
    account.deposit(newbalance);
    String test = String.valueOf(account.getBalance());
    result[i-1]  = test;
}

If you want the data to be deleted from the result, you can initialize result again:

result = new String[years];
Pang
  • 9,564
  • 146
  • 81
  • 122
FullStackDeveloper
  • 910
  • 1
  • 16
  • 40
  • `i` starts at 1, so you have the use `i-1` to set the value in `result` – Felix Jul 01 '17 at 22:33
  • why you put the years inside the String array? – Camila Jul 02 '17 at 15:59
  • Put years inside string array because since we already know the year, I use this year value to create a size of year value of string array. If your years is 10, it will create a size of 10's string array. – FullStackDeveloper Jul 02 '17 at 16:03