Recent

Author Topic: StrToInt conversion problem  (Read 3745 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1293
StrToInt conversion problem
« on: February 15, 2019, 09:20:41 pm »
Verifying fiekd within a line of text.

Within a line of text read from a text file  I have a field of 0000.

Var
Bit : String = '';
BitNbr : integer = -1;
begin
  // LineIn = '1 123 5487 0 0000 EFST
 // Verified that Bit = 0000
  Bit := ExtractWord(5,LineIn, ['  ']);
  Trim(Bit);
  BitNbr := StrToInt(Bit);

Execution error: at line xxx Not a valid integer.

The field should have Military time and will be between 0000 to 2400 but I need to verify. Is there a function or some way to do this.

 

     
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: StrToInt conversion problem
« Reply #1 on: February 15, 2019, 10:19:40 pm »
memo1.append(inttostr(strtoint(ExtractWord(5,LineIn, [' ']))));

works for me but it appears you have two spaces in your set of WordDelims.

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: StrToInt conversion problem
« Reply #2 on: February 16, 2019, 12:44:15 am »
memo1.append(inttostr(strtoint(ExtractWord(5,LineIn, [' ']))));

works for me but it appears you have two spaces in your set of WordDelims.

Davo


Here is the actual lines of code from the program.

//   AAA, PAJN , PANC , B734_ASA , 0020 , ASA7731 , ASA199 , 0 < Actual text line

   Bit := ExtractWord(5,LineIn, [',']);
   Trim(Bit);
   BitNbr := StrToInt(Bit);         <--- Error on this line '0000' not a valid integer
 
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

Zoran

  • Hero Member
  • *****
  • Posts: 1829
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: StrToInt conversion problem
« Reply #3 on: February 16, 2019, 12:54:47 am »

Trim is not procedure, but function, that is Trim(Bit); will do nothing.
But
Code: Pascal  [Select][+][-]
  1. Bit := Trim(Bit);
will actually do what you want.

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: StrToInt conversion problem
« Reply #4 on: February 16, 2019, 01:14:12 am »

Trim is not procedure, but function, that is Trim(Bit); will do nothing.
But
Code: Pascal  [Select][+][-]
  1. Bit := Trim(Bit);
will actually do what you want.

That did it.

Changed:

 Trim(Bit);    to    Bit  :=  Trim(Bit);

AAA, PAJN , PANC , B734_ASA , 0000 , ASA7731 , ASA199 , 0 < Actual text line

   Bit := ExtractWord(5,LineIn, [',']);

So the statement ExtractWord(5,LineIn, [',']); is picking up a space. Must  be before or after the 0000 field.

Thanks
   

FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: StrToInt conversion problem
« Reply #5 on: February 16, 2019, 04:38:27 am »
If you change your delimiter to a comma, then it will include the space as part of the word.

Try [' ', ',']    - maybe end of line markers too ?

there is a set of standard word delimiters, mentioned in ExtractWord page, would be a better approach .....

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: StrToInt conversion problem
« Reply #6 on: February 16, 2019, 04:47:30 am »
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: StrToInt conversion problem
« Reply #7 on: February 16, 2019, 06:45:57 am »
If you change your delimiter to a comma, then it will include the space as part of the word.

Try [' ', ',']    - maybe end of line markers too ?

there is a set of standard word delimiters, mentioned in ExtractWord page, would be a better approach .....

Davo

Yea, I got it. Thanks
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

 

TinyPortal © 2005-2018