I'm new to programming. When I compile this program, I get this error
message:
line 3: BankAccount(int) in BankAccount cannot be applied to ()
same error in Line 4. This one has me stumped. Can someone see
something I'm missing?
public class BankDemo {
public static void main(String[] args) {
BankAccount alice = new BankAccount(),
bob = new BankAccount();
alice.deposit(500);
bob.deposit(200);
alice.withdraw(100);
bob.deposit(300);
System.out.println("Alice's balance: " + alice.getBalance());
System.out.println("Bob's balance: " + bob.getBalance());
}
}
class BankAccount {
public BankAccount(int initialBalance) {
balance = initialBalance;
}
public void deposit(int amount) {
balance = balance + amount;
}
public void withdraw() {
balance = balance - 100;
}
public void withdraw(int amount) {
balance = balance - amount;
}
public int getBalance() {
return balance;
}
public String toString() {
return "BankAccount [balance=" + balance + "]";
}
private int balance = 0;
}
Rhino - 26 Nov 2005 05:18 GMT
> I'm new to programming. When I compile this program, I get this error
> message:
> line 3: BankAccount(int) in BankAccount cannot be applied to ()
> same error in Line 4. This one has me stumped. Can someone see
> something I'm missing?
Generally we'd have a lot better chance of helping if you gave the exact
error message, not a paraphrase. However, in this case, the problem seems to
be lines three and four are written with a comma between them. Change lines
three and four as follows and see what happens:
BankAccount alice = new BankAccount();
BankAccount bob = new BankAccount();
I think this will solve your problem.
Rhino
> public class BankDemo {
> public static void main(String[] args) {
[quoted text clipped - 36 lines]
> private int balance = 0;
> }
Lee Weiner - 26 Nov 2005 05:23 GMT
>I'm new to programming. When I compile this program, I get this error
>message:
[quoted text clipped - 12 lines]
> }
>}
The only constructor in class BankAccount requires that an int be passed as a
parameter to initialize the account to a balance. Your code in main() is
trying to construct two BankAccount objects without passing in the int
parameter:
BankAccount alice = new BankAccount( 1000 );
Lee Weiner
lee AT leeweiner DOT org
Roedy Green - 26 Nov 2005 16:46 GMT
use:
> BankAccount alice = new BankAccount(),
declaration:
> public BankAccount(int initialBalance) {
Don't you see the mismatch?

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.