String to integer in smalltalk?

Two steps: (a) validate the input, and (b) convert.

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.

Sir how about in dolphin, I don't use squeak. Can you explain it in dolphin syntax? Thanks a lot – leroj Aug 30 '10 at 8:19 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 Aug 30 '10 at 10:19 @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 Aug 31 '10 at 1:04 | 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 Aug 31 '10 at 1:07 What is your error @leroj? – mathk Aug 31 '10 at 5:52.

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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions