String conversion to Integer problems

blong

Active Member
Licensed User
Longtime User
I am reading text files of some 400 lines in length of say 50 characters a line.

I parse the lines using below into a string array

mybits= Regex.Split(",",myline)

A couple of the fields need conversation to Integers but the more I run the program, looping the same files and making different choices I start getting string errors

java.lang.StringIndexOutOfBoundsException
at org.apache.harmony.luni.util.FloatingPointParser.initialParse(FloatingPointParser.java:117)
at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:281)
at java.lang.Double.parseDouble(Double.java:287)

I used a simple expression myinteger = mybits(5) to implicitly do conversion

BUT whilst it initially, i.e. 1st pass of program, runs ok the more I loop the more errors I get. I used Try/Catch loops and simply try conversion a 2nd time and it works ! or even a 3rd ...

BUT this is terrible programming. Why JAVA wants to make simple strings like "123" a double before converting to Integer 123 I don't know and I remember seeing posts here about simple integer conversion.

It would seem to me to be some sort of "like a memory leak"... I keep consuming memory with string manipulation and then it gets very bad.

Is there a way to FORCE garbage collection of memory ?? or a better way to do the Integer conversion ??
 

blong

Active Member
Licensed User
Longtime User
Found a fix ! or workaround or whatever ... I still do NOT understand what is happening above and to me there is a bug in the underlying JAVA library

So my strings that I was converting were very simple 123, 201, 95 etc a number between 60 and 200 usually.

So string was "123" and using

myinteger = mystring caused problems after many conversions ( a few thousand)

so i did this

mystring = " " & mystring & " "

then myinteger = mystring

and it seems rock solid..... if I understand the code fix I use above this would allocate new memory for a new version of mystring ??

Maybe that's why it works...??. would really love to have VB like conversion routines that work like CInt(mystring)
 
Upvote 0

blong

Active Member
Licensed User
Longtime User
I wish I knew why there were errors...

I am passing through the same files each time and after multiple passes errors start to occur at random at different lines of the file. The strings I am trying to convert are OK . My fix above seems to have solved the problem and there is no reason to see why adding a blank character to either end of a string should affect the underlying conversion.

i.e. if mystr = "123" or " 123" or "123 " they should all result in an integer 123.

Would the fact that I am programming an Android 2.2 box have any bearing ??

I originally was using the android.jar file from 17 then switched to 8 but had same problem.

Any advice appreciated.

I may try and make a small program to test the string conversions and post it here. BTW love the new forum but miss "Search this forum" option.
 
Upvote 0

blong

Active Member
Licensed User
Longtime User
Erel ..... (and anyone else who can maybe explain...) ... :cool:

Spent the morning stripping down app to a very basic few lines and string to integer conversion problem still persisted ... thank goodness.

I attach a ZIP file which contains everything needed including the data file from which I read to get the strings.

Also in the ZIP file is a file called barry-logs.txt which are the logs I got from running program a couple of times.

It seems to me to some form of memory problem related to strings.

there are some comments in my simple lines of code that explain some variations I tried.

(BTW ... did not use your zip option in the IDE as it ignored my logs ... I stripped out apk file and classes etc to make under 512K)

Code window below.
B4X:
Sub Process_Globals

End Sub
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    '
    Dim txt_file As String, txt_line As String, txt_reader As TextReader
    Dim mysubs(100) As String, ipass As Int, make_it_work As Int, int_mins As Int
'    '
End Sub
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    '
    Activity.LoadLayout("frmMain")
    '
'    make_it_work=0    ' will crash on string conversion
'    make_it_work=1    ' will work fine
   
    make_it_work=1
    '
    For ipass=1 To 30
    load_selected_movie_list
    Next
    '
    make_it_work=0
    '
    '    crashes usually around 10-15 passes
    '
    For ipass=1 To 30
    load_selected_movie_list
    Next
    '
End Sub
Sub load_selected_movie_list
    '
    Dim dumstr As String
    '
    txt_file="movie_list.txt"
    '
    txt_reader.Initialize(File.OpenInput(File.DirAssets ,txt_file))
    '
    txt_line=txt_reader.ReadLine
    '
    Do While txt_line <> Null
    '
    mysubs=Regex.Split(",",txt_line)
    '
    dumstr=mysubs(5)
    '
    '    if I add line below then it works fine ...
    '    so must have something to do with the mysubs string
    '    or the Regex.split .... but when it crashes
    '    the Sub  cstr_int diagnostics show NICE numerical strings ...
    '
    '    memory pointers to strings must be getting lost ???
    '
'    dumstr="234"   
'
    int_mins=cstr_int(mysubs(5))
'
'    change to this line
'
'    int_mins=cstr_int(dumstr)
'
'    and still crashes
'
    txt_line=txt_reader.ReadLine
    '
    Loop
'
    txt_reader.close
    '
    Log("finished pass " & ipass)
'    '
End Sub
Sub cstr_int(mystr As String) As Int
'
    Dim myint As Int, mys As String
    mys= mystr
    If(make_it_work=1) Then mys=mys & " "
    '
    Try
        myint=mys
    Catch
        Log("string hassle >" & mys & "<  " & mys.Length)
        Try
            myint=mys
        Catch
            Log("tried again")
            myint=mys
            Log ("done 2nd time")
        End Try
        Log("done")
    End Try
    Return myint
'
End Sub
 

Attachments

  • StringTest.zip
    253.7 KB · Views: 264
Upvote 0

blong

Active Member
Licensed User
Longtime User
Erel

Eliminated Regex.Split by writing my own parser of input strings ... see code below

Trust you can point out where I am being DUMB ... coz it looks to me like JAVA bug ..

B4X:
Sub load_selected_movie_list
    '
    Dim dumstr As String, mypattern As String, mystr(10) As String
    '
    txt_file="movie_list.txt"
    mypattern=","
    '
    txt_reader.Initialize(File.OpenInput(File.DirAssets ,txt_file))
    '
    txt_line1=txt_reader.ReadLine
    txt_line2=txt_line1
    '
    Do While txt_line2 <> Null
    '
    'mysubs=Regex.Split(mypattern,txt_line2)
    '
    nfound=barry_parser(txt_line2,mysubs)
    dumstr=mysubs(5)
    '
    '    if I add line below then it works fine ...
    '    so must have something to do with the mysubs string
    '    or the Regex.split .... but when it crashes
    '    the Sub  cstr_int diagnostics show NICE numerical strings ...
    '
    '    memory pointers to strings must be getting lost ???
    '
'    dumstr="234"   
'
    int_mins=cstr_int(mysubs(5))
'
'    change to this line
'
'    int_mins=cstr_int(dumstr)
'
'    and still crashes
'

    txt_line2=txt_reader.ReadLine
    '
    Loop
'
    txt_reader.close
    '
    Log("finished pass " & ipass)
'    '
End Sub
Sub cstr_int(mystr As String) As Int
'
    Dim myint As Int, mys As String
    mys= mystr
    If(make_it_work=1) Then mys=mys & " "
    '
    Try
        myint=mys
    Catch
        Log("string hassle >" & mys & "<  " & mys.Length)
        Try
            myint=mys
        Catch
            Log("tried again")
            myint=mys
            Log ("done 2nd time")
        End Try
        Log("done")
    End Try
    Return myint
'
End Sub
Sub barry_parser(mystr As String, mybits() As String) As Int
'
    Dim indx As Int, start_indx As Int, inum As Int
    '
    indx=mystr.IndexOf(",")
    start_indx=-1
    inum=-1
    Do While indx >= 0 AND inum < 10
        inum=inum+1
        mybits(inum)=mystr.SubString2(start_indx+1,indx)
        start_indx=indx
        indx=mystr.IndexOf2(",",start_indx+1)
    Loop
    inum=inum+1
    mybits(inum)=mystr.SubString(start_indx+1)
    Return inum+1
    '
End Sub
 
Upvote 0

blong

Active Member
Licensed User
Longtime User
My ZIP file in message above contains all the files.

Please check and confirm.
 
Upvote 0

blong

Active Member
Licensed User
Longtime User
Thank you for feedback....

The device that my program is running on is an Egreat Media Player using Android 2.2 which is NOT a phone but a larger device designed to be hooked to a TV and have a hard disc full of media to watch/play.

I still wonder whether the problem is device dependent ...

I used the Android 2.2 SDK (Vers 8) for linking which is only some 5+ mbytes compared to 17 which is some 18 mbytes.

Maybe "my bug" is related to bugs in SDK version 8 ???

BUT my good news is I have a workaround ... add a "space" to the string and it works

"123" fails (sometimes) BUT "123 " seems to be unbreakable.

Why ??? :mad: AGAIN .... Thanks for testing ... it is appreciated.
 
Upvote 0

blong

Active Member
Licensed User
Longtime User
mc73 ... Thanks for suggestion ....

Sadly did not work... i think I maybe tried it before but can't remember.

But you made me think of something I had not tried ... using an emulator ! I have been using B4A Bridge to connect to my real device basically out of necessity to test my application. But my stripped down StringTest app is easily testable on an emulator...

and it WORKED FINE !! yuk ...

So my REAL device is an Egreat R200S which boots up to a multimedia player but one of the buttons on the player is an Android man (see attached image-done by phone) I click that and a message on screen appears "Android booting" and finally after a minute or so I get a new screen as also in images and in which I install my app. If I click settings I get following for my Android version

Model number Saturn Android version 2.2.1 Kernel 2.6.34-VENUS Build FRG83

Emulator says

Model number sdk Android version 2.2 Kerbel Version 2.6.29-00261-g0097074-dirty Build sdk-eng 2.2 FRF91

So now it points to a "poor" Android version ?? Device is Chinese and the settings page has only a few options (Location & Security, Applications, SD Card & Storage, Language & keyboard and About) as opposed to the emulator.

So Android Egreat version must be STRIPPED down and maybe too stripped and buggy !
egreat-open-screen.jpg egreat-android-booted screen.jpg
 
Upvote 0

blong

Active Member
Licensed User
Longtime User
Have found another workaround

Added some text to end of each line read... and again it works ... see code below...

So MUST be some form of problem with string handling in my Egreat hardware Android

B4X:
Sub load_selected_movie_list
    '
    Dim dumstr As String
    txt_file="movie_list.txt"
    mypattern=","
    '
    txt_reader.Initialize(File.OpenInput(File.DirAssets ,txt_file))
    '
    txt_line=txt_reader.ReadLine
    '
    Do While txt_line2 <> Null
    '
    '    add another field to end of line
    '    means mysubs now has 6 fields
    '    and it works fine
    '
    txt_line2=txt_line2 & ",ABC"
    '
    mysubs=Regex.Split(",",txt_line)
    '
    dumstr=mysubs(5)
    '
    int_mins=cstr_int(mysubs(5))
'
    txt_line=txt_reader.ReadLine
    '
    Loop
'
    txt_reader.close
    '
    Log("finished pass " & ipass)
'    '
End Sub
 
Upvote 0
Top