LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-05-2008, 04:00 PM   #1
raskol
Member
 
Registered: Mar 2008
Posts: 51

Rep: Reputation: 15
Check if token is an integer?


if i use raw_input() and want to see if the input is an integer, is there a way to do this?
 
Old 04-05-2008, 04:15 PM   #2
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,337

Rep: Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548
What language are you programming in? In general the way that you do this is to read the input into a floating point variable. Then copy the contents of the floating point variable into an integer variable which will truncate the decimal portion of the number. Copy the integer back into a floating point number and compare the two floating point numbers. If they are equal then the original number was an integer.

For example read in 4.3. Copy it to an integer variable where it is truncated to 4. Copy the integer to a floating point variable where it becomes 4.0. Compare 4.0 to 4.3 and the answer is that 4.3 is not an integer.

-------------------
Steve Stites
 
Old 04-05-2008, 04:29 PM   #3
raskol
Member
 
Registered: Mar 2008
Posts: 51

Original Poster
Rep: Reputation: 15
im using python.

the thing is not if its a float or an int.

i want to know if its an int/number or a char.


i found this:
http://python.active-venture.com/lib/module-string.html
This module defines some constants useful for checking character classes and some useful string functions. See the module re for string functions based on regular expressions.

digits
The string '0123456789'.

but dont understand how to use it.
 
Old 04-05-2008, 04:37 PM   #4
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Code:
value = raw_input()

try:
    value = int(value)
except ValueError:
    print "value is not an integer"
 
Old 04-05-2008, 04:53 PM   #5
raskol
Member
 
Registered: Mar 2008
Posts: 51

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Dan04 View Post
Code:
value = raw_input()

try:
    value = int(value)
except ValueError:
    print "value is not an integer"
ok ty but is that really a good way to do it?

i mean i could obv test for all if value == '1' or value == '2' etc but that is also kinda bad.
 
Old 04-05-2008, 05:04 PM   #6
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
That's what most Python programmers do. If you're adamant about not using exceptions, you could do (for non-negative values):

Code:
all(char in string.digits for char in value)
But "it's better to ask for forgiveness than permission."
 
Old 04-05-2008, 05:17 PM   #7
raskol
Member
 
Registered: Mar 2008
Posts: 51

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Dan04 View Post
That's what most Python programmers do. If you're adamant about not using exceptions, you could do (for non-negative values):

Code:
all(char in string.digits for char in value)
But "it's better to ask for forgiveness than permission."

i found this, it works and what was more what i was thinking of:

c=raw_input("yo: ")
if c in '0123456789':
print "integer"
else:
print "char"


however lets say the string is very large(WELL OBV NEVER IN THIS CASE BUT SAy i want see if it is "in" somethign else maybe its very expensive performance-wise?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C how to check if the input of scanf is an integer as opposed to a char? sycamorex Programming 1 02-07-2008 04:00 PM
howto check string is integer? ygloo Programming 4 10-14-2006 11:09 AM
token ring - how ? blish_blash Linux - Networking 1 08-17-2005 01:41 PM
Boot disk; check. CD in drive; check. Doesn't work; check. Hal DamnSmallLinux 7 02-04-2004 02:10 AM
Shell-Script check if $xx is of type integer d-fens Programming 5 07-11-2003 08:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration