simple question

hello everyone,

I am new to this board and have a simple question. How do i impose that a user input an integer? I guess it’s best to explain with an example. Lets say I have this simple code

set n to missing value
display dialog "Please enter an integer:" default answer ""
try
	set n to text returned of result as integer
end try
if n ≠ missing value then
	if n mod 2 = 0 then
		display dialog "The number is even"
	else
		display dialog "The number is odd"
	end if
else
	display dialog "You didn't enter an integer"
end if

as you can see entering a value of 1.5 would be converted to 2 after the class “as integer” is executed. Hence the number would be read as even. How do I correct this?

Hi grkness,

Use ‘as number’ instead of integer.

gl,

If you don’t want to round what was entered but insist that it be an integer, then:

repeat
	display dialog "Please enter an integer:" default answer ""
	try
		set n to text returned of result as number
	on error
		display dialog "That was not even a number"
	end try
	if n mod 1 = 0 then exit repeat
end repeat

This will only fall out of the loop if the entry is an integer

excellent. All integers are divisible by their prime… never thought of it that way. so

“n mod 1 = 0” is always true for an integer and false for a real number. Thank you :smiley: