Johan Fourie wrote:Hi again. I hope it is alright to post in same topic again.
Absolutely fine, providing you don't introduce a
new topic (which you haven't).
I was wondering how do I use [Winston's method] in my method? When I use the method, if the string is "not registered" it returns true and the validNumber(score) would be false. If the string is "232" it returns false and the validNumber(score) would be true.
My method is as follows...
My answer: Your new method is probably better. My goal was simply to show you how you might
validate a 'score' field - ie, write a method that returns true if a value is a valid score, and false if it isn't.
You've now thought about things and taken it a step further by applying it to your problem, and that was
exactly what I was hoping you would do. Your conclusion (as I see it) is that
validScore() is redundant, and
addToScore() is a much better way to proceed, and I can't fault your logic one bit.
So, well done. You get a +1 from me.
Programming is
thinking, not simply coding, and the chances are that most non-trivial programs you write will go through several revisions before you "get it right". You can reduce this somewhat by following the recommendations in the
StopCoding (←click) page, but programming is still an incremental process.
The only problem I can see - and it's not actually invalid; just redundant - is that having gone to all the effort to get an integer from your 'teams' array, you then convert it
back to a String. How about this as an alternative:
or, if you need to raise an error if the field is NOT valid:
There is also an alternative technique, described in
UserInput, that uses
Integer instead of
int and breaks up the process even more. You might find it useful, but don't worry about it too much if you feel you're getting swamped.
Remember: There are almost always MANY ways to solve a problem. Your first job is to get it
right. After that - and ONLY after that - you can then worry about making it fast or "slick".
HIH
Winston