Another Java Problem

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Daskill

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

Print out to the screen a list of those BankBalances that are overdrawn.
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?
 
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]
 

Daskill

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

I've tried doing it the following way:

if( currentAccounts .isInCredit() )
System.out.println("Accounts not in credit: " + name);


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

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...
 
Joined
Jul 29, 2004
Messages
6,650
Daskill said:
there's no such thing as a currentAccounts object in my program; currentAccounts is the name of the array...
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.
 
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.
 

Daskill

Thread Starter
Joined
Oct 17, 2007
Messages
295
/*
* BankAccountDriver.java Author: DM
*
* Demonstrates the use of
*/

package bankaccountproj;
import java.util.Scanner;
import java.io.*;

public class BankAccountDriver {

public static void main(String[] args) {

//Creates scanners
Scanner fileScan = null;
Scanner lineScan = null;

try{
fileScan = new Scanner(new File("BankAccountInput"));
}

catch(Exception e){
System.out.println(e);
}

//Reads array size from first line in file "BankAccount Input"
int arraySize = fileScan.nextInt();
BankAccount [] currentAccounts = new BankAccount[10];
fileScan.nextLine();



// To demonstrate the use of multiple constructors
BankAccount account1 = new BankAccount("Geoff", "123");
BankAccount account2 = new BankAccount("Helen","234", 100);
BankAccount account3 = new BankAccount("Jameel", "345", 700, 100);



//Creates int i. As long as i is less than arraySize, keep looping.
//i++ increments i after each loop
for (int i = 0; i < arraySize; i++)
{
//Scans next line in the file
String nextLine = fileScan.nextLine();
//Creates new lineScan Scanner
lineScan = new Scanner(nextLine);
//Sets "/" as the delimiter
lineScan.useDelimiter("/");
//Scans in the next int value and assigns it to the variable "type"
int type = lineScan.nextInt();
//Assigns next value to "name"
String name = lineScan.next();
//Assigns next value to "accountNumber"
String accountNumber = lineScan.next();

if(type == 3)
{
currentAccounts = new BankAccount(name, accountNumber);

System.out.println( "Account Name: " + name + " Account Number: " + accountNumber );
}
else if(type == 2)
{
double balance = lineScan.nextDouble();
currentAccounts = new BankAccount(name, accountNumber, balance);

System.out.println( "Account Name: " + name + " Account Number: " + accountNumber );
}
else if(type == 1)
{
double balance = lineScan.nextDouble();
double overdraft = lineScan.nextDouble();
currentAccounts = new BankAccount(name, accountNumber, balance, overdraft);

System.out.println( "Account Name: " + name + " Account Number: " + accountNumber );
}


//Can print out last name but how do you print out all names in array?
if( currentAccounts .isInCredit() )
System.out.println("Accounts not in credit: " + name);



// To demonstrate the use of accessor methods

System.out.println( account1 + " has a balance of " + account1.getBalance() );
System.out.println( account2 + " has an overdraft limit of " + account2.getOverdraftLim() );

if ( account3.isInCredit() )
System.out.println( account3 + " is not overdrawn." );
else
System.out.println( account3 + " is overdrawn." );


account1.deposit(200);
System.out.println( account1 + " now has a balance of " + account1.getBalance() );

String message = account2.withdraw(200);
System.out.println( message );

System.out.println( account3.withdraw(100) );
System.out.println( account3 + " now has a balance of " + account3.getBalance() );

// To demonstrate object interaction
System.out.println( account2.transfer( 50, account1 ) );
System.out.println( account1 + " now has a balance of " + account1.getBalance() );
System.out.println( account2 + " now has a balance of " + account2.getBalance() );
}
}
}
 
Joined
Jul 29, 2004
Messages
6,650
To answer your question here :
//Can print out last name but how do you print out all names in array?
if( currentAccounts .isInCredit() )
System.out.println("Accounts not in credit: " + name);


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]
 
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]
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

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 807,865 other people just like you!

Latest posts

Members online

Top