D dimitristsilis New Member Sep 10, 2015 #1 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: Code: Copy Dim number1 As Int number1 = "2" log(number1) Everything is ok. If I write: Code: Copy 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: Sep 10, 2015
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: Code: Copy Dim number1 As Int number1 = "2" log(number1) Everything is ok. If I write: Code: Copy 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!
DonManfred Expert Licensed User Longtime User Sep 10, 2015 #2 "2" is not an int. It is a string containing a quote, a 2 and another quote. You need to remove the quotes to cast it as an int. Or even just write a simple 2 to the file (without quotes) Upvote 0
"2" is not an int. It is a string containing a quote, a 2 and another quote. You need to remove the quotes to cast it as an int. Or even just write a simple 2 to the file (without quotes)
D dimitristsilis New Member Sep 10, 2015 #3 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: Sep 10, 2015 Upvote 0
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!
Computersmith64 Well-Known Member Licensed User Longtime User Sep 10, 2015 #4 The other option is you can cast the variable like this: Code: Copy 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
The other option is you can cast the variable like this: Code: Copy 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