• Post Reply Bookmark Topic Watch Topic
  • New Topic

Validating a String  RSS feed

 
Johan Fourie
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.
I am doing an assignment from Intec College. In this assignment I have a multiple dimension
String array named teams. At teams[2][0] there is a String with value "Pink", and at
teams[2][1] there is a String with value "not registered".
The first one is the team name and the second one is the score. I have to add 20 to the score.
I have 2 methods, validNumber() and editScore(int score).



I would like to know if that is an acceptable way to validate the score?
I am referring specifically to method validNumber() and the return false;
part in the catch block.
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the validNumber() method, if you are returning a boolean why need to throw NFE when you are catching it? Another thing you may want to consider is the array. Currently you are getting it from the class or instance variable I assume. But as a separate entity function, shouldn't you pass in the array, row and col to fetch?

 
Johan Fourie
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for your help. You assumed correctly. It is ok to say return false;
in the catch block? Previously I was not able to to think of the correct syntax for the
validNumber() method. Thank you for showing me the
part without the need to use as well. It greatly helps me.
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johan Fourie wrote:Thank you for showing me the <code> ... It greatly helps me.

IMO, I think you need to rationalize your method a bit.

If you look at your requirements, you have more than one thing going on:
1. You need to check if a String contains a valid number.
2. You have a field (which happens to be a String) that contains a team score, which can be either a number OR the String "not registered".
And those two things are quite different.

Presumably, you already know that your 'teams' array is a String[][] (otherwise, your original validNumber() method wouldn't compile), so why not create a generic method that simply checks whether a String is a valid number or not, eg:You could then re-use it to validate a score, viz:HIH

Winston

PS: Notice that the first method is static (and could actually be put in a utility class), whereas the second is not, because it is specific to your class, and also relies on your 'teams' array.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to wind up Winston, because I know how he likes the Scanner class My simplistic version will return true for the String "123 456 789", so needs some enhancements.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Just to wind up Winston, because I know how he likes the Scanner class

Hey, it's Friday. On Fridays, I can even take the "spawn of the devil" Scanner...

Winston
 
Johan Fourie
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the extra help. I must admit that I did not think of anything like this.
Hopefully I will be a bit better in the future.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome The advantage of a forum like this is that you get lots of people's opinions … and some of them are useful
 
Johan Fourie
Greenhorn
Posts: 14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again. I hope it is alright to post in same topic again. I was wondering how do I use 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

That method results in a NumberFormatException. I can put it in a try and catch block, but I thought it should not be necessary since I checked whether it is
a valid number.
The method that works without an exception is
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Johan Fourie
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Winston.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johan Fourie wrote:Thank you very much Winston.

You're most welcome. Good luck.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!