简体   繁体   中英

BlueJ Error "'Void' Type Not Allowed Here"

I am making a bank account program. It must hold information about the balance, name of the account owner, and the account number. It must also be able to withdraw and deposit money, check the balance, etc. It must also charge a $10 fee. I attempted to write this program, but when I run it, BlueJ says "'void' type not allowed here". Please help. This is my program below.

public class BankAccount
{
private double balance;

private String name;

private long accountNum;

public BankAccount(double initialBal, String owner, long number)
{
    balance = initialBal;
    name = owner;
    accountNum = number;
}

public void withdraw(double amount)
{
    if (balance >= amount)
    balance -= amount;
    else
    System.out.println("Insufficient funds");
}

public void deposit(double amount)
{
    balance += amount;
}

public double getBalance()
{
    return balance;
}

public void chargeFee()
{
    balance-=10;
}

public void changeName (String newName)
{

}
}




public class ManageAccounts
{
public static void main(String[] args)
{
    BankAccount acct1, acct2;

    acct1 = new BankAccount(1000, "Sam", 1111); //creates a new account

    acct2 = new BankAccount(500, "Alex", 2222); //creates a new account

    acct2.deposit(100); //deposits $100 into Sam's account

    System.out.println("Alex's balance is: " + acct2.getBalance()); //prints     balance

    acct1.withdraw(50); //withdraws $50 from Alex's account

    System.out.println("Sam's balance is: " + acct1.getBalance());

    System.out.println("Sam's balance is: " + acct1.chargeFee());

    System.out.println("Alex's balance is: " + acct2.chargeFee());

    acct2.changeName("Alexander"); //changes Alex's name to Alexander

    System.out.println( acct1 );

    System.out.println( acct2 );

}
} 

It is because you are calling the method: changeFee() on these lines:

System.out.println("Sam's balance is: " + acct1.chargeFee());

System.out.println("Alex's balance is: " + acct2.chargeFee());

And you are concatenating a String, in the first case "Sam's balance is: " with a void type, as the method chargeFee() don't return anything(void), this is not allowed. So this could be a quick fix if this is what you wanted:

public String chargeFee()
{
    return String.valueOf(balance-=10);
}

Compile it, run it and you get :

Alex's balance is: 600.0
Sam's balance is: 950.0
Sam's balance is: 940.0
Alex's balance is: 590.0
BankAccount@677327b6
BankAccount@14ae5a5

Also I see that you are not applying some coding conventions : http://www.oracle.com/technetwork/java/codeconvtoc-136057.html It is good that you get use to some good coding practices.

Also the method change changeName() is not doing anythig, you could change it like this:

public void changeName (String newName)
{
    name= newName;
}

And finally when you do this :

System.out.println( acct1 );

You are calling to the toString() method on the object acct1. See this : How to use the toString method in Java? for a better explanation. But the point is that you are not printing the names of the accounts holders. In order to do that you can create the following method:

public String getName(){
    return name;
}

And finally modify your last statements:

 System.out.println( acct1.getName());

 System.out.println( acct2.getName());

These statements will print the name of the account's holders.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM