brent bynum wrote:Came up with this. It just isn't working when studentID isn't 10 digits
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.
William P O'Sullivan wrote:Now.. What happens if the Id begins with 0? e.g: 0123456789
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
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:
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.
William P O'Sullivan wrote:Getting there..
print out the length() using:
You may be surprised ...
WP
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.
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
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.
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]
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
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.
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?
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.
your student ID should not be a numerical type if you're not going to use it as a number.
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.
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
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.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
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?
brent bynum wrote:
No, it does not.Jeff Verdegan wrote:Sounds like crappy requirements. In other parts, does it say anything about treating it as a number or as a String?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
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...
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.
Jeff Verdegan wrote:Same here for people treating something as a number just because it consists only of digits.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
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)?
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).
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?
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?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
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.
And actually, U.S. zip codes have a very strict numerical progression - why do you think they added the extra 4 digits?
Jeff Verdegan wrote:However, will your app ever know or care about the semantics of the specific numerical values?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
kunal
Kunal Lakhani wrote:This works for accepting a 10digit no only
kunal
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.
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.
Here. Have a potato. I grew it in my armpit. And from my other armpit, this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|