How to verify that a number is an integer from 0-1000?

4 views (last 30 days)
I am trying to verify that an input is an integer from 1-1000 in my code as shown below.
I am also verifying that there are no letters.
When I run this though I can input things like 2.1 or 123.24 and the code will still work and not get caught by the validation.
What function or code can I use to remedy this? I was trying stuff for length(str2num(x)) but that wasn't working for me.
Thank you!

Answers (3)

Image Analyst
Image Analyst on 26 Apr 2022
Here's one way
value = input('Enter an integer between 0 and 1000 : ')
r = rem(value, 1); % Get any fraction part to the right of the decimal point.
if r == 0 && value >= 0 && value <= 1000
fprintf('Input is good.\n');
else
fprintf('Input is out of range or has a fractional part.\n')
end
In r2021b (what I'm using) if you enter letters, it will automatically ask you again without needing to check for letters.
  3 Comments
Image Analyst
Image Analyst on 26 Apr 2022
Then my code should work perfectly for you as it did for me. There are no red errors whatsoever. The answer you ended up accepting uses the hated eval() which is strongly recommended against. You should not do it that way.
If you still have an error doing it my way, then show me how you altered my code to produce the red errors.
Walter Roberson
Walter Roberson on 26 Apr 2022
Edited: Walter Roberson on 6 Jun 2022
That code might give a yellow mark, warning that you should use semi-colon at the end of the assignment to value to prevent the value from accidentally displaying at the time of the assignment.
value = input('Enter an integer between 0 and 1000 : ');
r = rem(value, 1); % Get any fraction part to the right of the decimal point.
if r == 0 && value >= 0 && value <= 1000
fprintf('Input is good.\n');
else
fprintf('Input is out of range or has a fractional part.\n');
end
Also, be careful about whether exactly 0 is permitted or not.

Sign in to comment.


Walter Roberson
Walter Roberson on 26 Apr 2022
Trim off leading and trailing whitespace. Verify that the only remaining characters are '0' to '9'. After that take length() of the character representation. If it is 0 then fail. If it is 1 then fail if the string is '0' and otherwise pass. If length is 2 or 3 then pass. If it is 4 then pass if the string is '1000' and fail otherwise. If it is longer, fail.
However this algorithm will not work in the form described in any of these cases:
  • you want to permit unary plus, such as +74
  • you want to permit trailing decimal point not followed by anything
  • you want to permit trailing decimal point followed by 0s
  • you want to permit scientific notation.

Jason Shrand
Jason Shrand on 26 Apr 2022
This should do it:
row_selectionA = input('Your message here: ', 's');
while ~isgood(row_selectionA)
row_selectionA = input('ERROR: (Your error message here)', 's');
end
function result = isgood(s)
if isnan(str2double(s))
result = false;
else
% Result is numeric, so see if it's between 1 and 1000
val = eval(s);
result = val >= 1 && val <= 1000;
end
end

Categories

Find more on Characters and Strings in Help Center and File Exchange

Tags

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!