B4J Question Check if number is greater less

Discussion in 'B4J Questions' started by sigster, Apr 10, 2015.

  1. sigster

    sigster Active Member Licensed User

    Hi

    I am try to check if the app have new version
    this is my code I am getting error java.lang.NumberFormatException: multiple points

    Regards
    Sigster


    Code:
    Dim app_version As String = "1.0.1"   
        
    Dim upgrade As String


            
    If app_version < upgrade.check_upgrade Then
            Txt_yesorno.Text = 
    "Yes"
            
    Else
            Txt_yesorno.Text = 
    "No"
            
    End If

           
    Sub check_upgrade

    ConnectDatabase
           
    Dim RS As ResultSet = SQLCon.ExecQuery("SELECT * FROM version " WHERE ID = '1';")
    Do While RS.NextRow
       
        upgrade = RS.GetString(
    "version")
       
    Loop

    RS.Close

    End Sub
    Code:
    main._appstart (java line: 150)
    java.lang.NumberFormatException: multiple points
        at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:
    1110)
        at java.lang.Double.parseDouble(Double.java:
    540)
        at b4j.example.main._appstart(main.java:
    150)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
    57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
    43)
        at java.lang.reflect.Method.invoke(Method.java:
    606)
        at anywheresoftware.b4a.BA.raiseEvent2(BA.java:
    93)
        at anywheresoftware.b4a.BA.raiseEvent(BA.java:
    84)
        at b4j.example.main.start(main.java:
    36)
        at com.sun.javafx.application.LauncherImpl$
    5.run(LauncherImpl.java:319)
        at com.sun.javafx.application.PlatformImpl$
    5.run(PlatformImpl.java:219)
        at com.sun.javafx.application.PlatformImpl$
    4$1.run(PlatformImpl.java:182)
        at com.sun.javafx.application.PlatformImpl$
    4$1.run(PlatformImpl.java:179)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl$
    4.run(PlatformImpl.java:179)
        at com.sun.glass.ui.InvokeLaterDispatcher$
    Future.run(InvokeLaterDispatcher.java:76)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.access$
    100(WinApplication.java:17)
        at com.sun.glass.ui.win.WinApplication$
    3$1.run(WinApplication.java:67)
        at java.lang.Thread.run(
    Thread.java:745)
     
  2. derez

    derez Expert Licensed User

    The query
    Code:
    SQLCon.ExecQuery("SELECT * FROM version " WHERE ID = '1';")
    should be

    Code:
    SQLCon.ExecQuery2("SELECT * FROM version WHERE ID = ? ",Array As String(1))
    What is upgrade ? a module ? then it is wrong to dim it as a string. Do not use one name for many objects.
    The comparison of "<" between strings does not make too much sense.
     
    Last edited: Apr 10, 2015
    DonManfred likes this.
  3. Troberg

    Troberg Well-Known Member Licensed User

    Sure it does. It tells which of them sorts first in alphabetical order. "A"<"B", "Ape"<"Zebra", "2015-04-10"<"2015-05-11", "a111"<"a2" and so on.
     
  4. DarkMann

    DarkMann Member Licensed User

    Because your version number is actually a string (with 2 points in it, not 1 like a decimal number) you will have to use CompareTo on the strings.

    Code:
    Dim app_version As String = "1.0.1"  
    Dim upgrade As String = "1.0.2"  ' or whatever

        
    If app_version.CompareTo(upgrade)<0 then
            Txt_yesorno.Text = 
    "Yes"
            check_upgrade
        
    Else
            Txt_yesorno.Text = 
    "No"
        
    End If

    ' rest of code
    see http://www.b4x.com/b4j/help/core.html#string_compareto

    Like Troberg, I've always done it with > or < in other basics, but I guess it's something to do with the Java underpinnings of B4A and B4J that make that just not possible.

    David
     
    Troberg likes this.
  5. Troberg

    Troberg Well-Known Member Licensed User

    Wow, I've missed that. I don't think it breaks anything for me in my current code, but it's a nasty bug risk if you are not aware of it.

    In the sampe above, I'd simply test to see if the version is different from the current version. If, for some odd reason, the installed version is newer than the current version, something is very wrong and installing the current version seems prudent anyway.
     
    derez likes this.
  6. sigster

    sigster Active Member Licensed User

    Thanks for the help :)

    this is how I use it when I start the app I check for upgrade and if it is new upgrade I download the new file overwrite the app file and restart the app



    Code:
    Dim app_version As String = "1.0.1"  

    check_upgrade(app_version,upgrade)

    Sub check_upgrade(AppVersion As String,NewVersion As String)
              
    Dim job As HttpJob
            
    Dim NewFile As String
      
             
    If AppVersion.Compareto(NewVersion) < 0  Then
                    
    Log("Upgrade")
                    
    Dim job As HttpJob
                    
    Dim NewFile As String
                    NewFile = 
    "http://PATH-TO-THE-FILE"
                    job.Initialize(
    "DownloadFILE", Me)
                    job.Tag = NewFile
                    job.download(NewFile)
                 
    Else If AppVersion.Compareto(NewVersion) > 0 OR AppVersion.Compareto(NewVersion) = 0 Then                              
                    
    Log("No upgrade")
              
    End If
    End Sub
     
  7. derez

    derez Expert Licensed User

    Code:
    Else If AppVersion.Compareto(NewVersion) > 0 OR AppVersion.Compareto(NewVersion) = 0 Then
    Just Else will do the same...
     
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