i want to convert the input value in "Prompter prompt: aStringPrompt" into a integer value, how can i do that?
Two steps: (a) validate the input, and (b) convert.
You could validate like so: myString isAllDigits.
Converting is trivial: '1' asInteger. In Squeak, at least, this returns the integer 1. 'g1' asInteger returns 1, as does 'g1' asInteger. g asInteger returns nil.
So in summary:
"Given some input string s containing a decimal representation of a number, either return s in integer form, or raise an exception."
s := self getUserInput.
(s isAllDigits) ifFalse: [ Exception signal: '"', s, '" is not a (decimal) number' ].
^ s asInteger.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With