aspose file tools*
The moose likes Beginning Java and the fly likes Checking if input has ten numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Checking if input has ten numbers" Watch "Checking if input has ten numbers" New topic
Author

Checking if input has ten numbers

brent bynum
Greenhorn

Joined: Apr 04, 2012
Posts: 24
My program is asking the user to enter a student ID. This student ID should have ten digits(1234567890). What would be the best way to do it?

Code I have started:


William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

Clue:

Read or Convert to String

use the .length() method.

WP

Now.. What happens if the Id begins with 0? e.g: 0123456789
brent bynum
Greenhorn

Joined: Apr 04, 2012
Posts: 24
Came up with this. It just isn't working when studentID isn't 10 digits

William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

Getting there..

print out the length() using:


You may be surprised ...

WP
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
I would suggest that learning how to use a regular expression which checks for ten digits would be better. It will take a lot longer to learn, I am afraid.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
brent bynum wrote:Came up with this. It just isn't working when studentID isn't 10 digits

Strange. It worked for me.

Campbell Ritchie wrote:I would suggest that learning how to use a regular expression which checks for ten digits would be better. It will take a lot longer to learn, I am afraid.

I think regular expressions are overkill for this problem, and very likely contrary to the purpose of the exercise. The String length works fine, now he just needs to validate that the string is numeric, which is not difficult.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7823
    
  21

William P O'Sullivan wrote:Now.. What happens if the Id begins with 0? e.g: 0123456789

A very good point.

What's the answer brent? Does the ID have to be greater than 999999999, or is the 10 digits merely a display requirement?

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

brent bynum wrote:My program is asking the user to enter a student ID. This student ID should have ten digits(1234567890). What would be the best way to do it?

Code I have started:




You probably don't want to return an int there. For one thing, if IDs less than 1,000,000,000 are allowed (e.g., if 0 999 999 999 is allowed (minus the spaces)), then things get confusing, and the whole "has to be 10 digits" doesn't make sense, because ints don't have leading zeros.

Second, if you use an int your maximum ID values will be a little over 2,000,000,000.

Just like phone numbers and zip codes should not be represented by numerical types, because they're not numerical quantities, similarly, your student ID should not be a numerical type if you're not going to use it as a number.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18848
    
  40

Dennis Deems wrote:
Campbell Ritchie wrote:I would suggest that learning how to use a regular expression which checks for ten digits would be better. It will take a lot longer to learn, I am afraid.

I think regular expressions are overkill for this problem, and very likely contrary to the purpose of the exercise. The String length works fine, now he just needs to validate that the string is numeric, which is not difficult.


Kinda agree. Using regular expressions would be like bringing a gun to a knife fight.... On the other hand, it's nice to be able to validate the input *and* check for length at the same time. And if the validation gets more complex, you only have to recode the regex -- not the algorithm.



Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
I know I am in the minority, but maybe Henry’s last post with "[1-9][0-9]{9}" in suggests a regex isn’t quite such overkill after all. Can you use "\\d{10}" instead? [Edit]The latter regex would permit IDs beginning in 0[/edit]
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

I don't think a regex is overkill at all, though I agree that it may defeat the point of the exercise and not be appropriate in this particular setting.

If it were me though, doing this for real, I would absolutely, 100% use a regex. One of the following, depending on whether leading zeros are allowed:

brent bynum
Greenhorn

Joined: Apr 04, 2012
Posts: 24
William P O'Sullivan wrote:Getting there..

print out the length() using:


You may be surprised ...

WP


I did this but not sure exactly what it does. I noticed if it wasn't ten digits then my program would not go any further but if it had ten digits it would run completely.


Dennis Deems wrote:
brent bynum wrote:Came up with this. It just isn't working when studentID isn't 10 digits

Strange. It worked for me.


Explain? What worked?

Winston Gutkowski wrote:
William P O'Sullivan wrote:Now.. What happens if the Id begins with 0? e.g: 0123456789

A very good point.

What's the answer brent? Does the ID have to be greater than 999999999, or is the 10 digits merely a display requirement?

Winston


I am not sure what the answer is if the ID starts with 0. It has to be 10 digits. If it doesn't there should be some type of error.

Jeff Verdegan wrote:
brent bynum wrote:My program is asking the user to enter a student ID. This student ID should have ten digits(1234567890). What would be the best way to do it?

Code I have started:




You probably don't want to return an int there. For one thing, if IDs less than 1,000,000,000 are allowed (e.g., if 0 999 999 999 is allowed (minus the spaces)), then things get confusing, and the whole "has to be 10 digits" doesn't make sense, because ints don't have leading zeros.

Second, if you use an int your maximum ID values will be a little over 2,000,000,000.

Just like phone numbers and zip codes should not be represented by numerical types, because they're not numerical quantities, similarly, your student ID should not be a numerical type if you're not going to use it as a number.


Yeah, I noticed this after I compiled it.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7823
    
  21

Campbell Ritchie wrote:I know I am in the minority, but maybe Henry’s last post with "[1-9][0-9]{9}" in suggests a regex isn’t quite such overkill after all. Can you use "\\d{10}" instead? [Edit]The latter regex would permit IDs beginning in 0[/edit]

Take an infinite number of programmers, and eventually they'll write all the great works of Shakespeare...

I have to disagree with my esteemed colleague jverd here: To me, an ID (at least in this case; and generally I would say) is a number. It may not be a value, but unless I've missed something, it's definitely NOT a String.

We're also dealing with more than one thing:
1. Validation.
2. Display.
and I suspect (because he hasn't told us) that brent is confusing the first with the second.

If my student ID is 13 (and I'm probably old enough), why force me to put eight '0's in front of it? Classic case of introducing the possibility of error to me.
And if, indeed, the number must be 10 digits with no leading zeroes, then it seems to me that the easiest is to do the number conversion first and then check that it's > 999999999.

But that's me; old ornery DBA.

Winston
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

brent bynum wrote:
I am not sure what the answer is if the ID starts with 0. It has to be 10 digits. If it doesn't there should be some type of error.


Sounds like you need to get that clarified with your instructor.

Other than that detail though, where are you with your question? Have you figured it out?
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
If it were me doing this for real, I would use String.length followed by org.apache.commons.lang.StringUtils.isNumeric. (For those unfamiliar with this library, this method loops over the String's char array and tests whether each element is a digit.) I think the readability this way is better than using Regex. If the requirement were changed in the future so that student Ids were allowed to combine digits and characters, then I would probably switch to Regex.
brent bynum
Greenhorn

Joined: Apr 04, 2012
Posts: 24
Jeff Verdegan wrote:
brent bynum wrote:
I am not sure what the answer is if the ID starts with 0. It has to be 10 digits. If it doesn't there should be some type of error.


Sounds like you need to get that clarified with your instructor.

Other than that detail though, where are you with your question? Have you figured it out?


This is what this part of my program says to do:

Your program must include appropriate input validation:

Valid student ID numbers contain exactly 10 digits.


^^^^Instructions straight from my Assignment
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Winston Gutkowski wrote:
Campbell Ritchie wrote:I know I am in the minority, but maybe Henry’s last post with "[1-9][0-9]{9}" in suggests a regex isn’t quite such overkill after all. Can you use "\\d{10}" instead? [Edit]The latter regex would permit IDs beginning in 0[/edit]

Take an infinite number of programmers, and eventually they'll write all the great works of Shakespeare...

I have to disagree with my esteemed colleague jverd here: To me, an ID (at least in this case; and generally I would say) is a number. It may not be a value, but unless I've missed something, it's definitely NOT a String.


What I actually said was:
your student ID should not be a numerical type if you're not going to use it as a number.

which is guess is a little ambiguous. If the ID is your typical DB sequence number, then, yes, it's a number and should be treated as such. But if the input requirement is that it's exactly 10 digits long and allows leading zeros, then to me, that sounds more like a 10-character string that consists only of digits. Granted, if it has to be exactly 10 digits, then there's no problem with "01" and "001" both mapping to the numerical value 1.

I guess I'd say that requiring input of "exactly 10 digits, allow leading zeros, treat it as a number" is a faulty requirement.


We're also dealing with more than one thing:
1. Validation.
2. Display.
and I suspect (because he hasn't told us) that brent is confusing the first with the second.

If my student ID is 13 (and I'm probably old enough), why force me to put eight '0's in front of it? Classic case of introducing the possibility of error to me.


Right. If it's a number, then "has to be exactly 10 digits" makes sense as a display rule--i.e., pad with leading zeros. It doesn't make sense as an input validation rule however.

And if, indeed, the number must be 10 digits with no leading zeroes, then it seems to me that the easiest is to do the number conversion first and then check that it's > 999999999.


Agreed.

I think right now the requirement is either poorly specified or not totally understood, or both.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

brent bynum wrote:
Jeff Verdegan wrote:
brent bynum wrote:
I am not sure what the answer is if the ID starts with 0. It has to be 10 digits. If it doesn't there should be some type of error.


Sounds like you need to get that clarified with your instructor.

Other than that detail though, where are you with your question? Have you figured it out?


This is what this part of my program says to do:

Your program must include appropriate input validation:

Valid student ID numbers contain exactly 10 digits.


^^^^Instructions straight from my Assignment


Sounds like crappy requirements. In other parts, does it say anything about treating it as a number or as a String?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7823
    
  21

Jeff Verdegan wrote:But if the input requirement is that it's exactly 10 digits long and allows leading zeros, then to me, that sounds more like a 10-character string that consists only of digits.

Ah, callow youth that forgets the days when megabytes were measured in floor space...

And even if not, I still say that a 10-digit code is a number unless I've got a good reason to think otherwise. Part of that is an inbred aversion to using Strings simply because they're there. In my experience, more often than not they're simply the lazy man's alternative to defining a proper class.

grumble...grumble...

Winston
brent bynum
Greenhorn

Joined: Apr 04, 2012
Posts: 24
Jeff Verdegan wrote:
brent bynum wrote:
Jeff Verdegan wrote:
brent bynum wrote:
I am not sure what the answer is if the ID starts with 0. It has to be 10 digits. If it doesn't there should be some type of error.


Sounds like you need to get that clarified with your instructor.

Other than that detail though, where are you with your question? Have you figured it out?


This is what this part of my program says to do:

Your program must include appropriate input validation:

Valid student ID numbers contain exactly 10 digits.


^^^^Instructions straight from my Assignment


Sounds like crappy requirements. In other parts, does it say anything about treating it as a number or as a String?


No, it does not.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7823
    
  21

brent bynum wrote:
Jeff Verdegan wrote:Sounds like crappy requirements. In other parts, does it say anything about treating it as a number or as a String?
No, it does not.

Then I suggest you make a design decision. You've been given plenty of options.

Winston
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Winston Gutkowski wrote:
Jeff Verdegan wrote:But if the input requirement is that it's exactly 10 digits long and allows leading zeros, then to me, that sounds more like a 10-character string that consists only of digits.

Ah, callow youth that forgets the days when megabytes were measured in floor space...


I remember them, I just don't live in them any more.

And even if not, I still say that a 10-digit code is a number unless I've got a good reason to think otherwise.


In this case, the crappy, self-conflicting requirements put me on the fence, and I default to String, probably as a knee-jerk reaction to seeing too many people try to treat phone numbers as ints.

Part of that is an inbred aversion to using Strings simply because they're there. In my experience, more often than not they're simply the lazy man's alternative to defining a proper class.


Same here for people treating something as a number just because it consists only of digits.

I guess my stance isn't "It's a String," but rather, "don't assume it's a number just because it consists only of digits". And I'm going to keep harping on this point: Without proper requirements, at this point neither one is really any more "correct" than the other, since we don't even know what "correct" is in this context.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
Winston Gutkowski wrote: . . . Take an infinite number of programmers, and eventually they'll write all the great works of Shakespeare... . . .
And they are trying their hardest in this thread
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7823
    
  21

Jeff Verdegan wrote:Same here for people treating something as a number just because it consists only of digits.

Guess we'll have to agree to disagree there, because my general thinking would be that if something does consist entirely of digits and unless you're told otherwise, why wouldn't you assume that it's a number (and moreover, an integer)?

Obviously, with something like a phone number, you're dealing with a compound code, but there are still some advantages to dealing with each component as a number; the same with zip codes, CCN's, BAN's and SKUs (although I totally agree that they should all be encapsulated in classes).

Winston
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Winston Gutkowski wrote:
Jeff Verdegan wrote:Same here for people treating something as a number just because it consists only of digits.

Guess we'll have to agree to disagree there, because my general thinking would be that if something does consist entirely of digits and unless you're told otherwise, why wouldn't you assume that it's a number (and moreover, an integer)?


I agree that it's a natural assumption. I just don't think it makes sense to base one's code on it.

  • Does it represent an ordinal value? Is "123" the 123rd in the sequence, and does that matter to its semantics?
  • Will we be doing mathematical operations on it?


  • If the answer to both of those is no, then it's probably not appropriate to store it or represent it as a number.

    Obviously, with something like a phone number, you're dealing with a compound code, but there are still some advantages to dealing with each component as a number; the same with zip codes, CCN's, BAN's and SKUs


    "Hey, your zip code is twice mine, but my phone number is 500 larger than yours! From that we can conclude..." what, exactly? If you are the phone company or the post office, then there may be some cases where those numerical values or the relationships among them mean something, so maybe in those specific contexts it makes sense to store them as numbers but for general usage, not so much.

    If you make your zip code (post code) an int, and next year you want to ship to Canada, uh-oh. Change them to longs, and change the parse routines to radix 36?

    (although I totally agree that they should all be encapsulated in classes).


    Amen, brother.
    dennis deems
    Ranch Hand

    Joined: Mar 12, 2011
    Posts: 808
    brent bynum wrote:
    Dennis Deems wrote:
    brent bynum wrote:Came up with this. It just isn't working when studentID isn't 10 digits

    Strange. It worked for me.

    Explain? What worked?

    I ran the program. When student Id was 10 digits it was printed to the console. When it was not 10 digits, I was prompted for input. Of course, you weren't specific when you said "it isn't working", so it's possible some other behavior is desired than what I observed.
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 7823
        
      21

    Jeff Verdegan wrote:If you make your zip code (post code) an int, and next year you want to ship to Canada, uh-oh. Change them to longs, and change the parse routines to radix 36?

    Whoa, hoss. No zip codes in Canada. And actually, U.S. zip codes have a very strict numerical progression - why do you think they added the extra 4 digits?

    Winston
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    Winston Gutkowski wrote:
    Jeff Verdegan wrote:If you make your zip code (post code) an int, and next year you want to ship to Canada, uh-oh. Change them to longs, and change the parse routines to radix 36?

    Whoa, hoss. No zip codes in Canada.


    I know. Hence "post codes" in parens. My point is, you start out in only the U.S. You name your variables zipCode, type them as ints, and display "ZIP Code" next to the "5-spacer-4" box at the end of your address form. Next year you want to ship to Canada. "Oops, they have post codes (or whatever), not ZIP codes." No biggie. Change the text on your forms, maybe you do and maybe you don't change variable and method names, depending on how painful that is.

    Point being, the distinction between it being called "ZIP code" or "post code" or "yuubin-bangou" ("post number"--I think that's what they call them in Japan) is irrelevant. They all serve roughly the same purpose, and you can call it or name it whatever you want, and changing it where it really matters--the UI--is relatively easy.

    But if you've represented it as a number, you now have to change the type in a bunch of places--including probably the DB--and that can be uglier.

    And actually, U.S. zip codes have a very strict numerical progression - why do you think they added the extra 4 digits?


    Fair enough. I didn't know that. I thought the extra 4 were just for easier sub-dividing as the number of addresses grew.

    However, will your app ever know or care about the semantics of the specific numerical values?
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 7823
        
      21

    Jeff Verdegan wrote:However, will your app ever know or care about the semantics of the specific numerical values?

    I guess my thinking is that if somebody (presumably with more knowledge than us on its use) came up with a numeric code, then they probably had their reasons; the most obvious being that they didn't want '17' confused with '017' or any other "leading zero" alternate; ie, they wanted to use the number space. It therefore seems a bit presumptuous to assume that it isn't a "number" simply because it doesn't represent some sort of mathematical value.
    The other (and possibly old-fashioned; also maybe database-oriented) idea is that a number represents a relatively unconstrained set of unique values in a very small space.

    But also maybe the Thursday-night ramblings of an old codger.

    Winston
    Kunal Lakhani
    Ranch Hand

    Joined: Jun 05, 2010
    Posts: 622
    This works for accepting a 10digit no only




    edit - please don't provide full solutions


    kunal
    dennis deems
    Ranch Hand

    Joined: Mar 12, 2011
    Posts: 808
    Kunal Lakhani wrote:This works for accepting a 10digit no only

    Kunal, it's probably better to help brent reach his own solution, than to cook up your own and give it to him. Also, this approach will cause problems if the user enters certain kinds of unexpected input.
    Kunal Lakhani
    Ranch Hand

    Joined: Jun 05, 2010
    Posts: 622
    Oops. Sorry

    I Won't repeat this mistake again
    brent bynum
    Greenhorn

    Joined: Apr 04, 2012
    Posts: 24
    Dennis Deems wrote:
    brent bynum wrote:
    Dennis Deems wrote:
    brent bynum wrote:Came up with this. It just isn't working when studentID isn't 10 digits

    Strange. It worked for me.

    Explain? What worked?

    I ran the program. When student Id was 10 digits it was printed to the console. When it was not 10 digits, I was prompted for input. Of course, you weren't specific when you said "it isn't working", so it's possible some other behavior is desired than what I observed.


    That is odd...when I ran it it did not let me type in the ID again.
    Praveen Kumar M K
    Ranch Hand

    Joined: Jul 03, 2011
    Posts: 256
    Dennis Deems wrote:
    I ran the program. When student Id was 10 digits it was printed to the console. When it was not 10 digits, I was prompted for input. Of course, you weren't specific when you said "it isn't working", so it's
    possible some other behavior is desired than what I observed.


    Brent's program has a while loop which ends with a harmless semi-colon Did you notice that!
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Checking if input has ten numbers