Android Question how find whether a field has a value

Makumbi

Well-Known Member
Licensed User
B4X:
    Case "A", "B", "C", "D", "E", "O","F"
                                            Log(FieldName.Trim & " = " & FieldValue.Trim)


please is there a function in B4a which i can use to find out whether a given field has a value for example this FieldValue.Trim belcause i would only want to get out fields that has values and leave out the rest. please help

B4X:
Dim jt As HttpJob
                    jt.Initialize("", Me)
                        jt.Download("http://kccug.com/Generic_Handler_JSON/HandlerVBAlevelreport.ashx?customerid=" & Account.Text)
                    jt.GetRequest.Timeout = 10000 ' 10 seconds
                    Wait For (jt) JobDone(jt As HttpJob)
                    If jt.Success Then ' if job is success (http status code 200)
                        Dim RetVal As String
                        RetVal = jt.GetString
                        Log(RetVal)
                        
                        If jt.GetString = "[]" Then
                            'MsgboxAsync("No Records to Upload Yet for: " & CustID ,"SMIS")
                                Return
                            Else
                                
                                Dim parser As JSONParser
                                parser.Initialize(jt.GetString)
                                Dim root As List = parser.NextArray
                                For Each colroot As Map In root
                                    Dim FieldName As String
                                    Dim FieldValue As String

                                    For I = 0 To colroot.Size - 1
                                        Log(colroot.GetKeyAt(I) & " = " & colroot.GetValueAt(I))
                                    FieldName = colroot.GetKeyAt(I)
                                        Log(FieldName)
                                        If FieldName.Trim.ToUpperCase.EndsWith("POINTS") Then
                                    FieldValue = colroot.GetValueAt(I)
 
                                    Select Case FieldValue.Trim.ToUpperCase
                                                Case "A", "B", "C", "D", "E", "O","F"
                                            Log(FieldName.Trim & " = " & FieldValue.Trim)
          
                                                Case "1", "2", "3", "4", "5", "6""7", "8", "9", "10", "11", "12""13", "14", "15", "16", "17", "18", "19", "20"
                                            Log("  (Numeric score for " & FieldName.Trim & ")")
              
                                        Case "F"
                                            Log("  (No good at " & FieldName.Trim & ")")
          
                                    End Select
                                
                                    End If
                                    
                                Next
                                                                    
                                                jt.Release
            Return
                                
    
                Next   
            
                    
                End If
                End If

B4X:
[{"ACCOUNT":"05-00005","Name":"DDAMULIRA GEORGE","HISP":"","GPPoints":"F","PhyPoints":"","GeogPoints":"","EconPointst":"B","LitPoints":"","IREPoints":"","LugPoints":"","artpoints":"","FoodPoints":"","TDPoints":"","GermanPoints":"","WoodPoints":"","ArabPoints":"","EnterprPoints":null,"SubMathPoints":"F","CompPOINTS":"F","Principle":5,"Subsidiary":0,"TotalPoints":5}]
 
Last edited:

emexes

Expert
Licensed User
What Erel said, except I usually trim the value first, like you did. So, combining those two approaches:

B4X:
Dim FieldValues() As String = Array As String("hasvalue", "  ", "")
 
For Each S As String In FieldValues
    If S.Trim.Length <> 0 Then
        Log("""" & S & """" & ", length = " & S.Length & " : HAS VALUE")
    Else
        Log("""" & S & """" & ", length = " & S.Length & " : DOES NOT HAVE VALUE")
    End If
Next


and if you are expecting that most fields will be empty, and you're testing a lot of them, then it might be worth using:

B4X:
If S.Length <> 0 and S.Trim.Length <> 0 Then


which looks like it would take longer but because B4A/Java string transformations are incredibly expensive CPU-wise, but string length checks are cheap, and if the initial (cheap) check of the string length indicates that the untrimmed string is empty, then we can skip the (expensive) Trim operation.

(mind you, I haven't actually benchmarked the Trim operation, and it is entirely possible that it has some inbuilt shortcuts that mean it only does a string transformation when absolutely necessary, in which case the speed improvement will be minimal... hmm... I might just do that right now)

edit: righto, the trim takes ~ 2-to-4 times longer than the length check, depending on whether there are leading/trailing spaces or not, but on my PC with B4J we're talking about 100 ns vs 400 ns per check, so note the proviso: "and you're testing a lot of them" :)
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
On an probably-unrelated but possibly-frustrating issue... in the line

B4X:
Case "1", "2", "3", "4", "5", "6""7", "8", "9", "10", "11", "12""13", "14", "15", "16", "17", "18", "19", "20"

between 6 and 7, and 12 and 13, it looks like the comma has been left out, and thus the adjacent double-quotes become actual literal double-quotes between the numbers inside the string, ie, you are checking for the two strings 6"7 or 12"13 instead of the four strings 6 or 7 or 12 or 13
 
Upvote 0

Makumbi

Well-Known Member
Licensed User
On an probably-unrelated but possibly-frustrating issue... in the line

B4X:
Case "1", "2", "3", "4", "5", "6""7", "8", "9", "10", "11", "12""13", "14", "15", "16", "17", "18", "19", "20"

between 6 and 7, and 12 and 13, it looks like the comma has been left out, and thus the adjacent double-quotes become actual literal double-quotes between the numbers inside the string, ie, you are checking for the two strings 6"7 or 12"13 instead of the four strings 6 or 7 or 12 or 13
thanks for that observation
 
Upvote 0
Top