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

Dani AI

Generated

The compile error "This method must return a result of type String" comes from declaring public String toString() but not returning anything. As pointed out, toString() should build and return a String instead of printing to the console. A compact, safe implementation that also helps the compiler catch signature mistakes uses @Override and returns a formatted string:

@Override
public String toString() {
    return String.format("SavingsAccount[account=%s, branch=%s, name=%s %s, balance=R%.2f]",
                         account, branch, first, sur, balance);
}

After that, printing the object is simple and idiomatic:

System.out.println(mySavingsAccount);

Additional tips: keep toString() side‑effect free (do not call System.out.println inside it). If you need a human-readable console report, write a separate printReport() method that calls System.out.println(account.toString()). Using @Override helps catch typos (wrong signature or wrong case). If an IDE still flags an "overrides java.lang.Object.toString" message, verify the method signature exactly matches public String toString() and the project compiler compliance level is correct. Finally, wrapping posted code with code tags (as suggested) makes it easier for others to read and diagnose issues.

Recommended Answers

All 3 Replies

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.

Cool Ezzaral. I got it.
P.S. Sorry for the

(I don't know how to use that)... :(

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.