-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.

3
  • You are already appending in a container called result. Do you want to copy these values to another array? Commented Jul 2, 2017 at 10:26
  • yes I need that ? @VidorVistrom Commented Jul 2, 2017 at 15:58
  • stackoverflow.com/questions/9929321/… Commented Jul 2, 2017 at 18:35

1 Answer 1

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

3 Comments

i starts at 1, so you have the use i-1 to set the value in result
why you put the years inside the String array?
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.

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.