Android Question check if string is not > 0

leitor79

Active Member
Licensed User
Longtime User
Hi,

I have a string value stored in a KeyValueStore object. That value could be empty or have a long number type value. If this value is not a number greater than 0 (it could be 0, it could be empty, it could be nonsense since is a value I don't set) I want to perform something. If it is a number greater than 0, then it's OK and I don't have to do anything.

Remito has to be a string because is where I store a value coming from a webservice, which could be a string.

B4X:
Remito = Starter.Conf.GetDefault("remito", 0)
If Not(Remito > 0) Then
  .
  .
  .
End If

That code throws me an error: java.lang.NumberFormatException: Invalid double: ""
Also, if I declare an int or a long type variable to store the "starter.conf..." I get the same error.
"If Remito = 0" doesn't work either if Remito is an empty string value.

Which would be the correct way to make this?

Thank you!
 

emexes

Expert
Licensed User
One possibility is: if it is a number greater than zero, then:

- if it is an integer, then the first character must be "1" to "9"

- if it is a float, then the first character must be "0" to "9", or possibly a "." if you can have numbers like ".123" rather than "0.123"

But, more correctly and thoroughly, perhaps just use:

upload_2019-10-24_11-10-2.png


to first check that Remito is a number, before checking that it is not > 0.
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
More specific to your case:
B4X:
Remito = Starter.Conf.GetDefault("remito", 0)

Remito = Remito.Trim    'remove leading and trailing spaces (not mandatory, but is usually a good thing to do ;-)

If Remito.Length = 0 Then    'could also use: If Remito = "" Then
    Log("Remito is empty string")
Else If IsNumber(Remito) = False Then
    Log("Remito (" & Remito & ") is not a number")
Else If Not(Remito > 0) Then     'why not just Remito <= 0 ?
    Log("Remito is not positive (ie could also be zero)")
'Else
'    Log("Remito is positive")
End If
 
Last edited:
Upvote 0

leitor79

Active Member
Licensed User
Longtime User
Hi emexes, thank you for your answer.
I don't like that kind of solutions very much, since it's kinda... redundant. It would look this way:
B4X:
If Remito.Length = 0 Then    'could also use: If Remito = "" Then
    Call Function XX
Else If IsNumber(Remito) = False Then
    Call Function XX <---- the same call again
Else If Remito > 0 Then
    'do nothing
Else
    Call Function XX <--- for the third time
End If

Regards,
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
B4X:
If Not(IsNumber(Remito) And Remito > 0) Then
  Call Function XX
End If
Note: not tested
 
Upvote 0

emexes

Expert
Licensed User
it's kinda... redundant
Agreed, now that you've introduced Call Function XX to presumably fix up Remito if it is not a valid positive number. I had structured the tests sequentially that way on the assumption that different errors would be handled differently, eg an empty string might signify the end of data entry, and an invalid number might be a command.

In light of all errors being treated identically, I'd probably go more with:
B4X:
Dim FixFlag As Boolean = True    'set False if Remito is ok ie no fix required
If Remito.Length > 0 Then
    If IsNumber(Remito) Then
        If Remito > 0 Then
            FixFlag = False    'all good, no fix required
        End If
    End If
End If

If FixFlag Then
    Call Function XX    'fix Remito???
End If
which is longer but more straightforward than OliverA's answer; I liked that answer, except for the short-circuit evaluation... it is a great feature if used with care, but bites hard if you let your guard down ;-)
 
Upvote 0
Top