0

Hello, I am experiencing problems when trying to remove elements from an array.
I've been asked to create a little program which creates bank accounts containing (name, address, bank account number, balance etc.). I have successfully implemented all the functions except deleteAccount (I would also like to add that my array has a fixed size of 10 elemets).

Here's my method:

public void deleteAccount(String accountNumber)
    {
            
        for (int acc = 0; acc < nextAccountIndex; acc++)
        {
            if (accounts[acc].getAccountNumber().equals(accountNumber))
            {
                accounts[acc] = null;

                for (int remove = acc; remove < nextAccountIndex; remove++)
                {
                    accounts[remove] = accounts[remove+1];
                }       

            }

            accounts[nextAccountIndex-1] = null;
            nextAccountIndex--;
        }

    }

Whenever I create a new account, the accounts are created as it follows: 1001, 1002, 1003 etc., but when I try to delete account no: 1002 or 1003 I am left with just the first one. My question is: If I have filled the array with 10(or just 5) elements {1, 2, 3, 4, 5, etc.}, How can all the values move down, if I delete the value of the second or any other element in the array.
Thanks in advance!

2
Contributors
3
Replies
8
Views
5 Years
Discussion Span
Last Post by JamesCherrill
Featured Replies
  • Lines 17,18 are executed regardless of whether you have found the account to delete or not - they are outside the if test. Read More

1

Lines 17,18 are executed regardless of whether you have found the account to delete or not - they are outside the if test.

0

Lines 17,18 are executed regardless of whether you have found the account to delete or not - they are outside the if test.

Thank you very much for the quick reply! I've been trying to fix this 'error' for about an hour now. :D

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.