0

Good day!

I have a class Student with attribute String studentNo

Using OO, how can I validate if the input is an 11-digit no.

I can do this:

If(studentNo.length()<11){

}

to validate.. but how can I determine if it is a number or not.

And what are the other methods on how can I do this validation.

Any help would be highly appreciated. Thank you.

newbie
  • 14,582
  • 31
  • 104
  • 146

5 Answers5

5

You would want to use a regular expression, like this:

^[0-9]{11}$
Chris Eberle
  • 47,994
  • 12
  • 82
  • 119
3

You can check the following:

  • The length of the string is 11.
  • Each of the 11 character is a zero, a one, a two, a three, a four, a five, a six, a seven, an eight or a nine.

For a complex check you frequently store the result in a boolean. First setting it to true and if any of the conditions fail you set it to false. You then know in the end if all conditions held, by seing that it is still true.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
  • I would change the check so that is checks that the digit is larger or equal to '0' and less or equal to '9'. Another option would be to use Character.isDigit for all characters (if you accept that you then also will accept e.g. arabic digits) – Kaj May 09 '11 at 06:15
  • @Kaj, checking for a range assumes you are familiar with the unicode character map. I believe the OP to be too early in the learning process to be familiar with that. – Thorbjørn Ravn Andersen May 09 '11 at 06:27
2
bool isNum = true;
try {
    Integer.parseInt(studentNo);
}
catch (NumberFormatException e {
    // if we hit here it is not a number...
    isNum = false;
}
Aaron Gage
  • 2,373
  • 1
  • 16
  • 15
  • Is this a good practice? I thought Exceptiones should not be used for this purpose? – sarahTheButterFly May 09 '11 at 05:18
  • Does Java have a similar concept to .NET's `TryParse`? That'd eliminate the try/catch. – Matthew Scharley May 09 '11 at 05:18
  • @Sarah: It'd be nice to avoid it, but the simplest way to determine if a string is an integer is to ask the JVM. It's not ideal, but it's simple enough to understand and not really *that* bad. – Matthew Scharley May 09 '11 at 05:21
  • @Thorbjørn Ravn Andersen: presumably you would still do the length check first, then do this check for numericalness(?). – Matthew Scharley May 09 '11 at 05:22
  • @Matthew, see http://stackoverflow.com/questions/5932761/how-to-validate-if-studentno-is-a-11-digit-no/5932799#5932799 – Thorbjørn Ravn Andersen May 09 '11 at 05:52
  • In general using Exceptions for code flow is not a good thing to do, however in simple cases like this, as long as it is obvious what the code is doing, it is ok imho - @Matthew, tryParse would be ideal ;-) – Aaron Gage May 10 '11 at 09:51
0

Semantically, you could use

try {
    new BigInteger(studentNo);
} catch (NumberFormatException e) {
    ...
}

However, the regexp solutions might be more elegant with respect to the "don't use exceptions as a form of input checking" paradigm (exception handling is generally more expensive than simple checks). So, it depends on whether you want the code to be semantically clear or whether you want it to be efficient.

Piotr Wilkin
  • 3,446
  • 10
  • 18
0

Use regex:

http://en.wikipedia.org/wiki/Regular_expression

For example, in java you would do this:

studentNo.matches("[0-9]{11}");

sarahTheButterFly
  • 1,894
  • 3
  • 22
  • 36