tags:

views:

73

answers:

2

i want to convert the input value in "Prompter prompt: aStringPrompt" into a integer value, how can i do that?

+3  A: 

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.
Frank Shearar
sir how about in dolphin, i don't use squeak. can you explain it in dolphin syntax? thanks a lot
leroj
aString asInteger should work in all Smalltalks. The best way is to experiment a bit and look at the class library of your Smalltalk dialect to find out all possibilities.
Janko Mivšek
@Frank Shearar, thanks for the link but i that still did solve my problem. after the input dialog box appeared i entered my number but it doesn't change it to asInteger or to asNumber variable. my problem is after i declare my random number it doesn't change it here is my code please comment on it if you have a idea on how to solve it thank you
leroj
| dir |[ dir isNil or: [dir isEmpty]] whileTrue:[ dir:= Prompter prompt: 'Enter your number' caption:'Input the Number' ].MessageBox notify: 'your inputed number is ', (dir) caption: 'Inputed'.i want to make a simple "Guess the Number game" but for starters i want to convert the user input to anInterger type but it always have an error after i input my random number, i tried everything from asInteger, asNumber and nothing.. please help (in this program it only copies what i entered)
leroj
What is your error @leroj?
mathk
says does not understand #number, i don't remember much cause i forgot to save it
leroj
A: 

Just tried this in Dolphin 6:

(Prompter prompt: 'Enter a number') asInteger

Run this (place cursor on the above in a workspace and hit Ctrl-D), enter 123 in the prompt that comes up, and you'll see 123 displayed as the output. If you remove the #asInteger invocation, it'll display '123', indicating that a String was returned.

As to your 'does not understand #number', that means that somewhere in the code you were running the message #number as being sent to an object that didn't know how to handle it.

For the fun of it I took your code and slightly reformatted it:

| dir |

[ dir isNil or: [ dir isEmpty ] ] whileTrue:
    [ dir:= Prompter prompt: 'Enter your number' caption: 'Input the Number' ].

MessageBox notify: 'your inputed number is ', (dir) caption: 'Inputed'.

and found that it ran just fine. Then I noticed it didn't convert the returned String to a number, so I changed it to:

| dir |

[ ( dir isNil or: [ dir isEmpty ] ) or: [ (dir select: [ :c | c isDigit not ]) size > 0 ] ]  whileTrue:
    [ dir:= Prompter prompt: 'Enter your number' caption: 'Input the Number' ].

MessageBox notify: 'your inputed number is ', (dir) caption: 'Inputed'.

This also ran fine, with the added benefit that it won't accept non-numeric characters.

Share and enjoy.

Bob Jarvis