1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Another Java Problem

Discussion in 'Software Development' started by Daskill, Oct 30, 2007.

Thread Status:
Not open for further replies.
Advertisement
  1. Daskill

    Daskill Thread Starter

    Joined:
    Oct 17, 2007
    Messages:
    295
    I have an array, "currentAcounts". Each of the records in the array has a balance.

    I'm not entirely sure how I would do this. I tried using an If, but it didn't really fit, maybe I wasn't using it right?

    Any ideas?
     
  2. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    Hi Daskill,

    Your coding should look like something like this :

    Code:
    [SIZE=2]for (currentAccount ca : currentAccountArray) {
        if (ca.getBalance() < 0) {
                System.out.println("Account number " + ca.getNumber() + " balance : " + ca.getBalance());
        }
    }[/SIZE]
     
  3. Daskill

    Daskill Thread Starter

    Joined:
    Oct 17, 2007
    Messages:
    295
    For whatever reason, that didn't work.

    I've tried doing it the following way:



    (isInCredit is a boolean method declared elsewhere in the program which basically does what it says on the tin.

    Now, this method will return the last overdrawn account in the array but none of the others. How can I modify this function to return a full list of the overdrawn accounts?
     
  4. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    I would check if each item of the array really contains an currentAccounts object.

    Here is an example of the most common error Java beginners make with arrays, a classical case of NullPointerException :


    SomeObject[] array = new SomeObject[10];
    array[0].setValue(<method requirements>);


    Also, make sure that your currentAccounts object is 'arrayable'.
    For example, if your application is connecting to a SQL database, it is not a good idea to build an array of ResultSet objects in order to keep the data retrieved from the database.
    Indeed, the ResultSet object is 'alive' until the connection to the database is closed.
     
  5. Daskill

    Daskill Thread Starter

    Joined:
    Oct 17, 2007
    Messages:
    295
    there's no such thing as a currentAccounts object in my program; currentAccounts is the name of the array...
     
  6. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    You're right ! I should have talked with more precision about the type of objects contained in currentAccounts. ;)

    Look, it is not unusual to see ambiguous pieces of coding like this :
    String[] string = new String[15];
    I see it every day.
     
  7. Daskill

    Daskill Thread Starter

    Joined:
    Oct 17, 2007
    Messages:
    295
    So, how do I display every name in the array?
     
  8. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    To get a better idea of the problem, I suggest you to post the coding of the class you're using to build the currentAccounts array.
     
  9. Daskill

    Daskill Thread Starter

    Joined:
    Oct 17, 2007
    Messages:
    295
     
  10. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    To answer your question here :


    It can't be done within the current for loop as it is designed to scan each record of the BankAccountInput input file. Just after the scanning loop, you may insert a for loop to get the name of each BankAccount object that fits the isInCredit method.

    As you didn't post the class I needed, I've partially rebuilt it accordingly the logic you used in your BankAccountDriver :

    Code:
    [SIZE=2]
    public class BankAccount {
        private String name;
        private String accountNumber;
        private double balance;
        private double overdraftLimit;
        
        public BankAccount(String name, String accountNumber) {
            this.setName(name);
            this.setAccountNumber(accountNumber);
            setBalance(0.0);
            setOverdraftLimit(0.0);
        }
        public BankAccount(String name, String accountNumber, double balance) {
            this.setName(name);
            this.setAccountNumber(accountNumber);
            this.setBalance(balance);
            setOverdraftLimit(0.0);
        }
        public BankAccount(String name, String accountNumber, double balance, double overdraftLimit) {
            this.setName(name);
            this.setAccountNumber(accountNumber);
            this.setBalance(balance);
            this.setOverdraftLimit(overdraftLimit);
        }
        
        public boolean isInCredit() {
            if (balance < overdraftLimit) return true;
            else return false;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getAccountNumber() {
            return accountNumber;
        }
    
        public void setAccountNumber(String accountNumber) {
            this.accountNumber = accountNumber;
        }
    
        public double getBalance() {
            return balance;
        }
    
        public void setBalance(double balance) {
            this.balance = balance;
        }
    
        public double getOverdraftLim() {
            return overdraftLimit;
        }
    
        public void setOverdraftLimit(double overdraftLimit) {
            this.overdraftLimit = overdraftLimit;
        }
        
        public void deposit(double d) {
            balance = balance + d;
        }
        
        public String withdraw(double d) {
            balance = balance - d;
            if (balance < overdraftLimit) return "Warning !";
            else return "Ok !";
        }
        
        public String transfer(double d, BankAccount ba) {
            ba.deposit(d);
            return withdraw(d);
        } 
    }
    [/SIZE]
     
  11. Daskill

    Daskill Thread Starter

    Joined:
    Oct 17, 2007
    Messages:
    295
    How would that look?
     
  12. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    Code:
    [SIZE=2]
    for (int i = 0; i < currentAccounts.length; i++) {
        if( currentAccounts [i].isInCredit() ) {
            System.out.println("Accounts in credit: " + currentAccounts[i].getName());
        }
    }
    [/SIZE]
     
  13. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/645620

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice