This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Shnupbups's profile picture

Checking whether an input has a certain character in it?

Started by Shnupbups, 14 October 2012 - 02:05 AM
Shnupbups #1
Posted 14 October 2012 - 04:05 AM
For one of my projects I'm working on, (in case you were wondering, a 100% improved, complete revamp of that 'Guess the Number' game on the wiki) if you enter in one of the inputs a mixed numeral (e.g. 1.5, 7.8, 3.6 etc.) it will break the program. is there a way I can check whether the input has a decimal point/full stop in it or not? I REALLY need it (My dad keeps making it break on purpose 'cause he thinks it's funny), so if you could help that would be great. I might release the finished code to the public once it hits 3.0!

Thanks in advance,
[indent=3]Shnup :)/>/>[/indent]


(P.S. Thankyou dan200 for the colour api, my game looked boring in black and white)
Zoinky #2
Posted 14 October 2012 - 04:15 AM
You could just simply round the number down. Just use:


number = math.floor(number)

Make sure you change 'number' to whatever the input variable is.
Shnupbups #3
Posted 14 October 2012 - 04:31 AM
You could just simply round the number down. Just use:


number = math.floor(number)

Make sure you change 'number' to whatever the input variable is.

Thanks, but what if I want a message to display when they've entered a certain character.
remiX #4
Posted 14 October 2012 - 04:31 AM
Can't you check if the number is between a whole number, because if it is, it will have a decimal
Shnupbups #5
Posted 14 October 2012 - 04:47 AM
Can't you check if the number is between a whole number, because if it is, it will have a decimal
that would leave me having to do infinite code, as i would have to go:

if numbermin > 2 and numbermin < 3 then

if numbermin > 1 and numbermin < 2 then

if numbermin > 3 and numbermin < 4 then
etc. I would have to do it infinite times, as they enter what number it is so it can be anything.
remiX #6
Posted 14 October 2012 - 04:55 AM
Can't you check if the number is between a whole number, because if it is, it will have a decimal
that would leave me having to do infinite code, as i would have to go:

if numbermin > 2 and numbermin < 3 then

if numbermin > 1 and numbermin < 2 then

if numbermin > 3 and numbermin < 4 then
etc. I would have to do it infinite times, as they enter what number it is so it can be anything.

I didn't quite think about that before I posted :)/>/>
Shnupbups #7
Posted 14 October 2012 - 05:40 AM
I didn't quite think about that before I posted :)/>/>

Easy mistake to make. Thanks anyway.
PixelToast #8
Posted 14 October 2012 - 05:44 AM
derp?
math.floor(val)==val
sould work :3
Shnupbups #9
Posted 14 October 2012 - 05:58 AM
derp?
math.floor(val)==val
sould work :3
No, not 'derp'. I have barely ever used math equations in my previous programs. Hell, nearly all of my previous programs have involved a password to get into something. I am new to all this math.(insert thing here) crap. What does it even do anyway? I can't learn anything if i don't know why it does what it does. Oh, and you spelled should wrong.
PixelToast #10
Posted 14 October 2012 - 06:03 AM
derp?
math.floor(val)==val
sould work :3
No, not 'derp'. I have barely ever used math equations in my previous programs. Hell, nearly all of my previous programs have involved a password to get into something. I am new to all this math.(insert thing here) crap. What does it even do anyway? I can't learn anything if i don't know why it does what it does. Oh, and you spelled should wrong.
math.floor rounds down and basically removes everything in the decimal place
a similar function math.ceil rounds up, so if there are any decimals, it will add up instead
both of these functions are verry usefull
Shnupbups #11
Posted 14 October 2012 - 06:07 AM
math.floor rounds down and basically removes everything in the decimal place
a similar function math.ceil rounds up, so if there are any decimals, it will add up instead
both of these functions are verry usefull
As I have said before:

Thanks, but what if I want a message to display when they've entered a certain character.
PixelToast #12
Posted 14 October 2012 - 06:10 AM
then you need to recode the read function to check for certain chars
put your code somewhere under the os.pullEvent function
Shnupbups #13
Posted 14 October 2012 - 06:12 AM
then you need to recode the read function to check for certain chars
put your code somewhere under the os.pullEvent function
Oh God…
This… may take a while…
PixelToast #14
Posted 14 October 2012 - 06:18 AM
*puts on sad, grayscale face :C*
Shnupbups #15
Posted 14 October 2012 - 06:35 AM
*puts on sad, grayscale face :C*
*watches toasty move away to a place called the infinite sadness*
Zoinky #16
Posted 14 October 2012 - 08:01 AM
He's trying to figure out how to check for a decimal place. It's not exactly helpful to tell him to recode it if he doesn't know how.
PixelToast #17
Posted 14 October 2012 - 08:32 AM
He's trying to figure out how to check for a decimal place. It's not exactly helpful to tell him to recode it if he doesn't know how.
You could just simply round the number down. Just use:


number = math.floor(number)

Make sure you change 'number' to whatever the input variable is.

Thanks, but what if I want a message to display when they've entered a certain character.
Lyqyd #18
Posted 14 October 2012 - 08:52 AM
You can use string.find().


input = read()
if string.find(input, "%.") then
  print("You entered a period!")
end
Shnupbups #19
Posted 14 October 2012 - 09:49 AM
You can use string.find().


input = read()
if string.find(input, "%.") then
  print("You entered a period!")
end


THANKYOU!!! You helped me SO much! :)/>/> :)/>/> :)/>/> :)/>/>

You are AWESOME!
[indent=3]Shnup :D/>/>[/indent]
Zoinky #20
Posted 14 October 2012 - 09:55 AM
He's trying to figure out how to check for a decimal place. It's not exactly helpful to tell him to recode it if he doesn't know how.
You could just simply round the number down. Just use:


number = math.floor(number)

Make sure you change 'number' to whatever the input variable is.

Thanks, but what if I want a message to display when they've entered a certain character.

I made a suggestion. I didn't say "Go re-code it.". Not the same.
remiX #21
Posted 14 October 2012 - 11:24 AM
You can use string.find().


input = read()
if string.find(input, "%.") then
  print("You entered a period!")
end

That's quite an awesome thing to use. A thanks from me too :)/>/>
Cloudy #22
Posted 14 October 2012 - 11:29 AM
To be honest rather than blacklisting any character you should check if tonumber() works on it first - if it is nil then it is not a number. You should then check that if you math.floor() it that it is the same number - if it's not then the number is invalid.
Doyle3694 #23
Posted 14 October 2012 - 01:36 PM
if you're looking for non decimal values(Don't really know what you're looking for, the comments are confusing), use

if Input % 1 ~= 0 then
– code here
end
Shnupbups #24
Posted 15 October 2012 - 10:25 AM
To be honest rather than blacklisting any character you should check if tonumber() works on it first - if it is nil then it is not a number. You should then check that if you math.floor() it that it is the same number - if it's not then the number is invalid.
I have used tonumber() so that it only takes in numbers, not words. But it takes in 8.3 or 1.9 or 5.4 as a number, so tonumber() didn't stop that. i was trying to make it print some text on the screen etc. when you entered in a decimal point inside your input somewhere. This has now be solved and I am requesting a topic lock.
PixelToast #25
Posted 15 October 2012 - 02:33 PM
put [SOLVED] in the front of the title, and it will be buried in the forums
less work for the moderators