Author | Message |
---|---|
VictorE
Posts: 3
|
Posted 17:21 Oct 12, 2018 |
If we ask the user for a number it is easy to check if they input a String but If we ask the user for a String such as a first name, they can input a number and it will still go through as a String. Is there an exception we can use to check this? |
knagrec2
Posts: 51
|
Posted 17:23 Oct 12, 2018 |
I think that may be ok, as some people have strange names (possibly with numbers). So as such there should not be any real content limitations on name beyond basic input checks (not blank) etc. |
kknaur
Posts: 540
|
Posted 18:04 Oct 12, 2018 |
Generally speaking, when you ask the user for numeric information, you should always read and store it as a number. Numbers take up far less space than Strings in general so to store everything as a String is very inefficient. If you read a number that is a String, you can always convert it to a number using one of the parse methods from the wrapper classes. If the parsing throw an exception (which it will if the String is not a number). You can catch that exception. |
knagrec2
Posts: 51
|
Posted 19:20 Oct 13, 2018 |
Does this mean that if there is a number given in the name entry, for example, it is not possible for a number to be included in the name? We have to check to see that numbers are not being entered where strings ought to be? |
kknaur
Posts: 540
|
Posted 20:21 Oct 13, 2018 |
Generally yes, you would check for something like this, but since the menu system is already going to be a lot of work, you can optionally check for numbers where the data should be characters only, just to make the assignment a little more manageble. Again, good thinking though, this is the kind of error checking that you should always be considering as a programmer. |