Android Question Can't get integer from file?

dimitristsilis

New Member
Hello everyone! I'm using b4a trial 5.20.
The problem: I want to get the integer "2" from the first line of a .txt file, but...
If I write:
B4X:
Dim number1 As Int
  number1 = "2"
log(number1)
Everything is ok. If I write:
B4X:
Dim line As String  
line = TextReader1.ReadLine  
TextReader1.Close  

Dim number1 As Int
  number1 = line
log(number1)
it throws an exception: java.lang.NumberFormatException: Invalid double: "2"
"2" is the number that the text file contains. There is only this number in the file, nothing else.
What could be the problem? Thanks for reading!
 
Last edited:

dimitristsilis

New Member
Sorry, my bad! I just used the quotes to show that 2 is the integer and not some random number... I never used the quotes in the code, nor in the file!
 
Last edited:
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
The other option is you can cast the variable like this:

B4X:
Dim line AsString 
line = TextReader1.ReadLine 
TextReader1.Close 

Dim number1 As Int
number1 = cInt(line)
log(number1)

Private Sub cInt(o as Object) as Int
    Return Floor(o)
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…