Strange error message

A

aver24

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;
}
 
R

Rhino

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) {
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;
}
 
L

Lee Weiner

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();
}
class BankAccount {
public BankAccount(int initialBalance) {
balance = initialBalance;
}
}

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top