This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
You are displaying the BankAccount object. If you want to see something meaningfull then you need to override the toString() method of the BackAccount class. Assuming the "1000" represent the value of a field called accountNumber. The toString() method might look something like:
You can also call a method on the bank account to return a string, or more than one method, and call those. The point is that println expects something that it understands, and if you just hand it a random object, it uses toString on it.
So it could be
and toString would not have to be defined on BankAccount in that case.
Ralph Cook wrote:
...and toString would not have to be defined on BankAccount in that case.
I don't think it is a good idea.
From the API docs of toString()
Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.
As you can see it makes lots of sense to override the toString()
Oh, one last thing, don't try to scan text with nextLine(); AFTER using nextInt() with the same scanner! It doesn't work well with Java Scanner, and many Java developers opt to just use another Scanner for integers. You can call these scanners scan1 and scan2 if you want.
I would personally always input strings, then error-check for input of an int before proceeding.
Joined: Oct 13, 2005
The problem with Scanner is that next line means "from wherever we are at present until the next end-of-line character". If you enter a number 123 then push enter, the remainder of the line is very short (a 0-character String). You may need a nextLine() call which you ignore. I wrote about that here a couple of years ago. It is something not well covered in the books I have read.
I couldn't get the previous link to open: try this instead if you have the same problem.