How to convert stored numbers from a text file

jb27

New Member
Licensed User
Longtime User
Hi,

Please help

problem 1 :
I have a text file.
In this file, there are numbers.
Each line is one number, a float.

I can read these numbers but only as text.
That should not be a problem but, I need this floats as floats.
Normally, in VB, I use csng(mystring) and it's ok.

problem 2 :
When I will be able to have my floats as floats,
I want to use Array As Float (my_float_1_in_my_txt, my_float_2_in_my_txt, my_float_3_in_my_txt, my_float_4_in_my_txt,my_float_1_in_my_txt...................)

But I can not find the way to put all these floats from my txt in this array.
 

jb27

New Member
Licensed User
Longtime User
solution for problem 1

It is easy to solve problem 1

When you use file.readline and use a variable, it must not be a string but a list :

Dim line As List
Dim Reader As TextReader
Reader.Initialize(File.OpenInput(File.DirAssets , "jb.txt"))
line=Reader.Readlist
Reader.Close

now, you can get your float :

Dim myarrayoffloat () As Float
myarrayoffloat = Array As Float (line.Get (0),line.Get (1))

but now, I cannot find how to ask my array to get all of my list value.
I have tried it, but it doesn't work :


For mycycle=0 To line.Size-1
myarrayoffloat = Array As Float(line.get(rotor))
Next

has anybody the solution ?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The Array As Float statement should be used when you want to fill an array with static data. This is not the case here.
You should use code similar to:
B4X:
Dim list1 As List
list1 = File.ReadList(File.DirAssets, "jb.txt")
Dim floats(list1.Size) As Float
Dim s As String
For i = 0 To list1.Size - 1
 s = list1.Get(i)
 floats(i) = s 'Strings are converted to floats automatically
Next
 
Upvote 0
Top