Android Question Automatic "cast" ??

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi.
I am experiencing a strange "bug". I get the following error, when trying to put a string value, just read with a TextReader, to an integer (or double) variable:

java.lang.NumberFormatException: Invalid double: "5"

The code is:
B4X:
#Region  Project Attributes 
 #ApplicationLabel: B4A Example
 #VersionCode: 1
 #VersionName: 
 'SupportedOrientations possible values: unspecified, landscape or portrait.
 #SupportedOrientations: unspecified
 #CanInstallToExternalStorage: False
#End Region
#Region  Activity Attributes 
 #FullScreen: False
 #IncludeTitle: True
#End Region
Sub Process_Globals
 'These global variables will be declared once when the application starts.
 'These variables can be accessed from all modules.
  Dim N As Int
 Dim D As Double 
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.
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")
 If FirstTime=True Then
 Dim NomeFileLocale As String :  NomeFileLocale="Smas/ListaCaixaN_UTF8.txt"
 If File.Exists(File.DirRootExternal, NomeFileLocale) Then
 Dim TextReader1 As TextReader
 TextReader1.Initialize2(File.OpenInput(File.DirRootExternal, NomeFileLocale),"UTF8")
 Dim sz As String
 sz = TextReader1.ReadLine.Trim ' this reads "5"
 D=sz ' <<< error HERE (originally I need an integer variable
 N=sz ' but I try also with a double, and see that doesn't depend on Integer or double)
 TextReader1.Close
 End If
 End If
 
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
The same doesn't happen if i force the string value to : sz="5", for example.
What am I missing? Thanks in advance.
 

DonManfred

Expert
Licensed User
Longtime User
remove the two " from the string.
The cast to int or double will work then

it should read
5
from the file

and not
"5"
 
Last edited:
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
No, not so simple. As a matter of fact I wrote "5", looking at the Log, but, seeing the debugger ToolTip, i just see 5:

upload_2017-4-4_16-13-4.png


So, I think that, actually, we have a double character. As a matter of fact I added:
B4X:
sz=sz.SubString(1)
after reading the line, and now it works.
By the way: how to replace a single " in a string? It seems not so immediate, at least for me...
Thanks for help.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi. The file is big, and the part that I already posted, which is its beginning, is enough for testing.
But I noticed that things aren't as I think. As a matter of fact, just first 5 has the problem we talked about.
The file is organized in the following way:
NumberOfElements ( Therefore a number)
.. Elements .. (strings)
..........
NumberOfElements (number)
.. etc.

First of all I inform you that, unluckily, applying your suggestion doesn't resolve (with the very first number 5 of the file).
B4X:
Sub Activity_Create(FirstTime As Boolean)
 'Do not forget to load the layout file created with the visual designer. For example:
 'Activity.LoadLayout("Layout1")
 If FirstTime=True Then
 Dim NomeFileLocale As String :  NomeFileLocale="Smas/test_char.txt"
 If File.Exists(File.DirRootExternal, NomeFileLocale) Then
 Dim TextReader1 As TextReader
 TextReader1.Initialize2(File.OpenInput(File.DirRootExternal, NomeFileLocale),"UTF8")
 Dim sz As String,l As Int
 
 sz = TextReader1.ReadLine.Trim
 sz.Replace($"""$,"") ' <<<<<<< taking away the "
 'N=sz ' ERROR
 
 For i=0 To 4 ' << FORCED to read following strings, till next NumberOfElements: SHOULD Be N-1, BUT N=sz gives error..
 sz=TextReader1.ReadLine.Trim
 Next
 
 sz = TextReader1.ReadLine.Trim
 
 N=sz ' this works.. so the problem is ONLY in the very first NumberOfELements (I hope)
 
 TextReader1.Close
 End If
 End If
 
End Sub

Seen with OpenOffice, Just for a brutal sight of the situation, we see that only first 5 has problems..:

5
Não Conhecido 0
Circular 1
Misto 2
Quadrado 3
Rectangular 4
5
Não Conhecido 0
Alvenaria de Pedra 1
Alvenaria de tijolo 2
An├Òis pre-fabricado de bet├úo 3
Betão armado 4
4
Não Conhecido 0
Dom├Òstico 2
Pluvial 3
Unitario 1
10
etc.

Finally seen with an hex editor, we see:

ef bb bf 35 20 20 20 20 20 20 20 20 20 20 20 20 0d 0a <<<<<< Here the 5 has "ef bb bf" before.. Therefore problems ...
4e c3 a3 6f 20 43 6f 6e 68 65 63 69 64 6f20 20 20 30 20 0d 0a
43 69 72 63 75 6c 61 72 20 20 20 20 20 20 20 20 31 0d 0a
4d 69 73 74 6f 2020 20 20 20 20 20 20 20 20 20 32 0d 0a
51 75 61 64 72 61 64 6f 20 20 20 20 20 20 20 20 33 0d 0a
52 65 63 74 61 6e 67 75 6c 61 72 20 20 20 20 20 34 0d 0a
35 0d 0a << here clearly just one char (5) so NO PROBLEM
etc.

This is the situation.. Thanks for your interest.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi, Erel. The output of your instruction is all the file, with no strange things:

** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
5
Não Conhecido 0
Circular 1
Misto 2
Quadrado 3
Rectangular 4
5
Não Conhecido 0
Alvenaria de Pedra 1
Alvenaria de tijolo 2
Anéis pre-fabricado de betão 3
Betão armado 4
4
Não Conhecido 0
Doméstico 2
Pluvial 3
Unitario 1
10
Não Conhecido 0
Ante-projecto 3
Estudo prèvio 2
Levantamento local 5
Obras 10
Oficina2 8
Plantas de cadastro 6
Projecto de execução 4
Telas finais 7
Topografia 9
32
Não Conhecido 32
Alcolombal 17
Alfaquiques/Sacário 20
Alfouvar 13
Almargem do Bispo 12
Almoçageme 23
Almorquim 16
Aóia 25
Arneiro da Arreganha 15
Barcarena 4
Cabrela 10
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
I used NotePad, having seen that it was the possibility to save with UTF8. I don't have NotePad++, because very first time I meet such a problem..
Here is the file.
 

Attachments

  • test_char.txt
    1.5 KB · Views: 280
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Probably just not to use TextReader for this type of file, as you suggest. I was just thinking that it doesn't matter the encoding..
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
I will use the File.ReadString option. I need to have such data in memory, so, the list of lines is a way to keep them, perhaps not the best, but may work in practice.
Thanks
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Yes, Manfred. I think that you got it: it exists, among the many possibilities, and now the beginning of the file has not those "disturbing" (forgive my ignorance) characters.
Much probably it will work now, without problems. Here is the updated file.
Thanks again.
 

Attachments

  • test_char.txt
    1.5 KB · Views: 286
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
it exists, among the many possibilities, and now the beginning of the file has not those "disturbing" (forgive my ignorance) characters
Remember this setting for the future. It should be THE Encoding for files you want to read on Android.
 
Upvote 0
Top