* determining input data type
@ 2008-10-18 0:48 jedivaughn
2008-10-18 1:41 ` Jeffrey R. Carter
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: jedivaughn @ 2008-10-18 0:48 UTC (permalink / raw)
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
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: determining input data type
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 18:54 ` anon
2 siblings, 1 reply; 7+ messages in thread
From: Jeffrey R. Carter @ 2008-10-18 1:41 UTC (permalink / raw)
jedivaughn wrote:
> 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.
You could use Integer'Value. If it succeeds, than it represents an Integer. If
it fails, then it doesn't.
--
Jeff Carter
"Why don't you bore a hole in yourself and let the sap run out?"
Horse Feathers
49
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: determining input data type
2008-10-18 1:41 ` Jeffrey R. Carter
@ 2008-10-18 6:17 ` Paul
0 siblings, 0 replies; 7+ messages in thread
From: Paul @ 2008-10-18 6:17 UTC (permalink / raw)
Or you could test for characters in the range '0' .. '9'. You'll still need
to wrap it up inside an exception handler.
Paul
"Jeffrey R. Carter" <spam.jrcarter.not@spam.acm.org> wrote in message
news:M8bKk.392682$yE1.130775@attbi_s21...
> jedivaughn wrote:
>> 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.
>
> You could use Integer'Value. If it succeeds, than it represents an
> Integer. If it fails, then it doesn't.
>
> --
> Jeff Carter
> "Why don't you bore a hole in yourself and let the sap run out?"
> Horse Feathers
> 49
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: determining input data type
2008-10-18 0:48 determining input data type jedivaughn
2008-10-18 1:41 ` Jeffrey R. Carter
@ 2008-10-18 6:53 ` deadlyhead
2008-10-18 8:00 ` Dmitry A. Kazakov
2008-10-18 18:54 ` anon
2 siblings, 1 reply; 7+ messages in thread
From: deadlyhead @ 2008-10-18 6:53 UTC (permalink / raw)
On Oct 17, 5:48 pm, jedivaughn <jedivaugh...@gmail.com> wrote:
> 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
Exceptions are a wonderful thing:
-- file: int_test.adb
-- Tests to see if an input string is an integer
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
procedure Int_Test is
My_Int : Integer;
begin
Put("Give me some input, and I will tell you if it is an integer:
");
Get(My_Int);
Put(My_Int, 0);
Put(" is an integer!");
New_Line;
exception
when Data_Error =>
Put("That was not an integer!");
New_Line;
end Int_Test;
Using Ada.Integer_Text_IO.Get does some work for you, though. For
instance, if the user inputs 214yy0, it puts 214 in My_Int and
continues on its way. This may not be what you want, thus some actual
text processing may be in order.
-- deadlyhead
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: determining input data type
2008-10-18 6:53 ` deadlyhead
@ 2008-10-18 8:00 ` Dmitry A. Kazakov
0 siblings, 0 replies; 7+ messages in thread
From: Dmitry A. Kazakov @ 2008-10-18 8:00 UTC (permalink / raw)
On Fri, 17 Oct 2008 23:53:54 -0700 (PDT), deadlyhead wrote:
> Using Ada.Integer_Text_IO.Get does some work for you, though. For
> instance, if the user inputs 214yy0, it puts 214 in My_Int and
> continues on its way. This may not be what you want, thus some actual
> text processing may be in order.
Yes, this was one of the motivations behind:
http://www.dmitry-kazakov.de/ada/strings_edit.htm
Your example reworked for Strings_Edit:
with Ada.Text_IO; use Ada.Text_IO;
with Strings_Edit.Integers; use Strings_Edit.Integers;
procedure Int_Test is
My_Int : Integer;
begin
Put ("Give me some input, and I will tell you if it is an integer:");
My_Int := Value (Get_Line);
Put_Line (Image (My_Int) & " is an integer!");
exception
when Constraint_Error =>
Put_Line ("That was a too large integer!");
when End_Error =>
Put_Line ("You entered nothing!");
when Data_Error =>
Put_Line ("That was not an integer!");
end Int_Test;
The function Value takes care to check the whole string for containing
integer and nothing but integer.
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: determining input data type
2008-10-18 0:48 determining input data type jedivaughn
2008-10-18 1:41 ` Jeffrey R. Carter
2008-10-18 6:53 ` deadlyhead
@ 2008-10-18 18:54 ` anon
2008-10-20 11:01 ` jedivaughn
2 siblings, 1 reply; 7+ messages in thread
From: anon @ 2008-10-18 18:54 UTC (permalink / raw)
--
-- 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
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: determining input data type
2008-10-18 18:54 ` anon
@ 2008-10-20 11:01 ` jedivaughn
0 siblings, 0 replies; 7+ messages in thread
From: jedivaughn @ 2008-10-20 11:01 UTC (permalink / raw)
thanks all, got it working just fine with your help
- John
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2008-10-20 11:01 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2008-10-20 11:01 ` jedivaughn
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox