From: anon@anon.org (anon)
Subject: Re: determining input data type
Date: Sat, 18 Oct 2008 18:54:10 GMT
Date: 2008-10-18T18:54:10+00:00 [thread overview]
Message-ID: <mhqKk.78582$Mh5.40529@bgtnsc04-news.ops.worldnet.att.net> (raw)
In-Reply-To: d39e1918-0c74-40b5-860d-f8d93181afd2@p59g2000hsd.googlegroups.com
--
-- Without knowing what format your data is in it is kind of
-- hard to decide what way is the best. Some ways are easy
-- while others are more complex
--
-- But if the data is a packed buffer string then you could use
-- Ada.Characters.Handling.Is_Digit.
--
-- In this case the best was is to use a loop and search for all
-- valid data where the search routine would find the integer
-- value and no error occurs unless the data is float. Also in this
-- example, only base 10 integers are valid.
--
with Ada.Text_IO ;
use Ada.Text_IO ;
with Ada.Integer_Text_IO ;
use Ada.Integer_Text_IO ;
with Ada.Characters.Handling ;
use Ada.Characters.Handling ;
procedure b is
Data_Error : exception ;
function To_Integer ( Buffer : String ) return Integer is
Index : Natural ;
First : Natural ; -- first valid digit
Last : Natural ; -- Last valid digit
begin -- To_Integer
Index := Buffer'First ;
while Index <= Buffer'Last and then
not is_digit ( Buffer ( Index ) ) loop
Index := Index + 1 ;
end loop ;
First := Index ;
while Index <= Buffer'Last and then
is_digit ( Buffer ( Index ) ) loop
Index := Index + 1 ;
end loop ;
Last := Index - 1 ;
-- check for decimal point aka the number is float
if Index <= Buffer'Last and then
Buffer ( Index ) = '.' then
raise Data_Error ;
end if ;
return Integer'Value ( Buffer ( First..Last ) ) ;
exception
when Data_Error =>
Put_Line ( "Data was invalid -- need to check data source" ) ;
Put_Line ( "Invalid Data element =>" & Buffer ( Index ) ) ;
raise ;
end To_Integer ;
Data : integer ;
begin -- b
Data := To_Integer ( " =123;" ) ;
Put ( "Data => " ) ;
Put ( Data ) ;
New_Line ;
-- Data_Error is raised
Data := To_Integer ( "asv456" ) ;
Put ( "Data => " ) ;
Put ( Data ) ;
New_Line ;
-- Data_Error is raised
Data := To_Integer ( " =789.;" ) ;
Put ( "Data => " ) ;
Put ( Data ) ;
New_Line ;
exception
when Data_Error =>
null ;
end b ;
In <d39e1918-0c74-40b5-860d-f8d93181afd2@p59g2000hsd.googlegroups.com>, jedivaughn <jedivaughn14@gmail.com> writes:
>Hi, How is the best way to determine the type of input. I have the
>input coming in through a string but if the contents of the string is
>an integer then I want to convert it to such. This seems like it
>should be very easy to do but I can't seem to find out how to do it
>any where.
>
>
>Thanks,
>
>John
next prev parent reply other threads:[~2008-10-18 18:54 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-10-18 0:48 determining input data type jedivaughn
2008-10-18 1:41 ` Jeffrey R. Carter
2008-10-18 6:17 ` Paul
2008-10-18 6:53 ` deadlyhead
2008-10-18 8:00 ` Dmitry A. Kazakov
2008-10-18 18:54 ` anon [this message]
2008-10-20 11:01 ` jedivaughn
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox