1

this is my array of objects, because its not allowed to use arraylist and this is is the one i know. I just used 3 for me to be able to practice before making it bigger

veco[] accountList = new veco[3];
        accountList[0] = new veco(accountNumber,billCode,billDate,accountName,accountAddress,customerNumber, periodTo,periodFrom,presentReading,previousReading,previousBalance,dueTotal);
        accountList[1] = new veco(accountNumber,billCode,billDate,accountName,accountAddress,customerNumber, periodTo,periodFrom,presentReading,previousReading,previousBalance,dueTotal);
        accountList[2] = new veco(accountNumber,billCode,billDate,accountName,accountAddress,customerNumber, periodTo,periodFrom,presentReading,previousReading,previousBalance,dueTotal);

this is the case where I look for the same account number. when i use the accountNumber that is stored in accountList[0], it will check the accountNumber. But when I use the accountNumber except the accountList[0] it will go directly towards the else which says Account ID doesn't exist in the database

System.out.print("\nEnter Account Number: ");                                                                                                                    
int b = scanner.nextInt();                                                                                                                                       
for (int j = 0; j < accountList.length; j++) {                                                                                                                   
    if (b == accountList[j].getAccountNumber()) {                                                                                                                
        System.out.println("Customer Account Number: " + accountList[j].getAccountNumber());                                                                     
        System.out.println("Customer Account Name: " + accountList[j].getAccountName());                                                                         
        System.out.println("Customer Account Address: " + accountList[j].getAccountAddress());                                                                   
        System.out.print("Customer Number: " + accountList[j].getCustomerNumber());                                                                              
                                                                                                                                                                 
        System.out.println("\nBilling Code\t\tBilling Date\t\tAmount Due");                                                                                      
        for (j = 0; j < accountList.length; j++) {                                                                                                               
            if (b == accountList[j].getAccountNumber()) {                                                                                                        
                System.out.println(accountList[j].getBillCode() + "\t\t\t" + accountList[j].getBillDate() + "\t\t\t" + (df.format(accountList[j].getDueTotal())))
            }                                                                                                                                                    
        }                                                                                                                                                        
    } else {                                                                                                                                                     
        System.out.print("Account ID doesn't exist in the database.\n");                                                                                         
        break;                                                                                                                                                   
    }                                                                                                                                                            
}                                                                                                                                                                
chptr-one
  • 600
  • 2
  • 15
Dean Yankee
  • 29
  • 1
  • 6
  • Does this answer your question? [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – daniu Apr 21 '21 at 09:54
  • accountNumber is int, and im using == instead of of .equals() the first one just works fine but the next accountNumbers is not being read and automatically goes to else – Dean Yankee Apr 21 '21 at 09:57
  • 1
    After I correct the code formatting, did you see the problem? Look carefully what happens when the cycle does not find an account. Instead of going further the cycle is interrupted. Please format the code correctly. Respect indents. – chptr-one Apr 21 '21 at 09:59
  • thank you so much, i used the break that's why it stops the cycle. i fixed my code and it works now. thank you so much for the help @chptr-one – Dean Yankee Apr 21 '21 at 10:03
  • @Deen Please look at my answer below. This is the preferred method of iteration on the collection. You do not need an index and such code looks much clearer. – chptr-one Apr 21 '21 at 10:13

1 Answers1

0

You can use iterator pattern

for (Account account : accountList) {                           
    if (account.getId() == userInputID) {                       
        // do the work 
        break;                                 
    }                                                           
}                                                               
System.out.print("Account ID doesn't exist in the database.\n");
chptr-one
  • 600
  • 2
  • 15
  • i will try this one, i have addition question if its okay. how do i for example i want to delete accountList[1] – Dean Yankee Apr 21 '21 at 10:13
  • @Dean If you want to remove the element at a time when you iterate over collection, you must have an `iterator` and use `remove()` method. See https://stackoverflow.com/questions/10431981/remove-elements-from-collection-while-iterating – chptr-one Apr 21 '21 at 10:16
  • thank you @chptr-one, ill go check it out. but it is applicable for array of objects? ive made the same ccode using arraylist but our instructor wont allow us using the arrylist and wants us to use arrays only but she allowed us to use array of objects – Dean Yankee Apr 21 '21 at 10:19
  • @Dean Yes, you can use for-each loop over arrays. But removing elements may not be so simple. What does "delete an array element" mean? Do you need to shift all the items after removed element to the left? Or after removing the item, you should get a new array in which there will be no removed element? – chptr-one Apr 21 '21 at 10:25
  • @Dean It seems to me that your instructor wants you to write your own simple implementation of `ArrayList`. This is a very useful task. Look at how the `ArrayList` is implemented in the standard Java library, it will help you very much. There is not a hard code, it is easy to read and understand. – chptr-one Apr 21 '21 at 11:08