Hi guyz,
I''ve done a bank's savings account program in java but can't compile, I keep on gettin the errors.
1. overrides java.lang.Object.toString
2. This methode must return a result of type String.

both of the errors are on the method public String toString () {...

public class SavingsAccount {
private String password,branch,sur,first,account;
private int amount;
private double balance = 0;
public void initialiseAccount (String accountNumber, String initPassword, String branchName, String surname, String firstNames) {
account = accountNumber;
password = initPassword;
branch = branchName;
first = firstNames;
sur = surname;
}
public String toString() {
System.out.println ("Account Number: "+account);
System.out.println ("Branch: "+branch);
System.out.println ("Name: "+first+" "+sur);


}
public boolean withdrawCash (int amount) {
boolean result = false;
if (amount > balance)
System.out.println("Insufficient funds");
else {
balance -= amount;
System.out.println("Withdrawal successful");
result = true;
}
return result;
}
public void depositCash (int amount) {
balance += amount;
System.out.println("Deposit successful");
}
public void changeUserPassword (String oldPassword, String newPassword) {
if (oldPassword.equals(password))
System.out.println("Password change successful");
else
System.out.println("Old password does not match");
}
public double printBalance () {
if (balance == 0)
System.out.printf("Balance: R"+"%.2f",balance);
else {
System.out.printf("Balance: R"+"%.2f",balance);
}
System.out.println();
return balance;
}
}

Edited 3 Years Ago by happygeek: fixed formatting

Because your toString() method is not returning a string, it's just printing stuff out to the console. It needs to build a string and return it.

Also, after 18 posts, you should learn to use [code] [/code] tags around code that you post.

P.S. Sorry for the (I don't know how to use that)... :(

Just place [code] before and [/code] after your code, or you can highlight the code and click the little toolbar button to "Wrap CODE tags around selected tags" (the one that shows #). It will preserve the tab and space formatting and make it much easier to read.

This question has already been answered. Start a new discussion instead.