Android Question Can't get integer from file?

Discussion in 'Android Questions' started by dimitristsilis, Sep 10, 2015.

Similar threads

B4A Tutorial [B4X] Smart String Literal
B4A Tutorial Text files
B4A Tutorial Android Serial tutorial
B4A Library [B4X] B4XFormatter - Advanced number formatter
B4A Tutorial Flickr Viewer
  1. dimitristsilis

    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:
    Code:
    Dim number1 As Int
      number1 = 
    "2"
    log(number1)
    Everything is ok. If I write:
    Code:
    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
  2. DonManfred

    DonManfred Expert Licensed User

    "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)
     
    Ed Brown and RandomCoder like this.
  3. dimitristsilis

    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: Sep 10, 2015
  4. Computersmith64

    Computersmith64 Well-Known Member Licensed User

    The other option is you can cast the variable like this:

    Code:
    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
     
  5. dimitristsilis

    dimitristsilis New Member

    It worked! Many thanks Computersmith64!
     
    Computersmith64 likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice