If "19:00:00" > "190001" Then I get an error

awama

Active Member
Licensed User
Longtime User
Why get I an error if I compare two strings?

test1 = "19:00:00"
test2 = "190001"

If test1 > test2 Then .... I get an error like this

java.lang.NumberFormatException: Invalid double: "19:00:00"

What I am doing wrong?
Thanks for solution
Walter
 

awama

Active Member
Licensed User
Longtime User
It's a casting issue. Do you have test1 and test2 dimmed as a String before assigning the values?

Yes, I have dimmed as Strings before.
The same effect has it if I try without variables like this:
If "19:00:00" > "190001" Then ...
 
Upvote 0

awama

Active Member
Licensed User
Longtime User
OK, that was it. Many thanks.

Where can I look which signs are not allowed in strings
for operations/compare?

Sorry for my bad english. If I will get time I will visit my cousin in Califonia for learning better english...
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
OK, that was it. Many thanks.

Where can I look which signs are not allowed in strings
for operations/compare?

Sorry for my bad english. If I will get time I will visit my cousin in Califonia for learning better english...

Since usually we need to compare numbers or strings, I don't think that a search is necessary. Just format your types equally and that should resolve comparisons. Margret's formula for replacement should do the trick in most occasions, while in others you can use the other functions suggested.
 
Upvote 0

awama

Active Member
Licensed User
Longtime User
Now I try this and I got the same error:

This are values which I read from a textfile into a list
19:00:00
19:05:00
19:10:00
... an so on

test1, test2 are dimmed as string

test1 = DateTime.Time(DateTime.now)
test2 = list1.Get(i)

If test1.Replace(":", "") > test2.Replace(":", "") Then ... I got same Error

java.lang.NumberFormatException: Invalid double: "19:00:00"
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Now I try this and I got the same error:

This are values which I read from a textfile into a list
19:00:00
19:05:00
19:10:00
... an so on

test1, test2 are dimmed as string

test1 = DateTime.Time(DateTime.now)
test2 = list1.Get(i)

If test1.Replace(":", "") > test2.Replace(":", "") Then ... I got same Error

java.lang.NumberFormatException: Invalid double: "19:00:00"

Try dimming your numbers before the comparison:
B4X:
dim comp1, comp2 as double
comp1=test1.replace(":","")
comp2=test2.replace(":","")
if comp1>comp2 then
 
Upvote 0

BarrySumpter

Active Member
Licensed User
Longtime User
LOL.

Now that is weird.

B4X:
Dim t1 as string
Dim t2 as string

    t1 = "19:00:00"
    t2 = "19:00:01"

if t1 = t2 then
   'do if equal
else
  ' do if not equal
end if
works ok

but
B4X:
Dim t1 as string
Dim t2 as string

    t1 = "19:00:00"
    t2 = "19:00:01"

if t1 > t2 then
   'do if Greater
else
  ' do if not Greater
end if
Doesn't work. With NumberFormatException

I have to agree.
I think this is a bug.
Is there not something b4a developers can do?
 
Last edited:
Upvote 0

awama

Active Member
Licensed User
Longtime User
Solution

Ok, after the post from thedesolatesoul I remember me to a string-function:
string1.CompareTo(string2), so it is able to compare date & time strings

Dim test1, test2 As String

test1 = "19:00:00"
test2 = "19:10:00"

If test1.CompareTo(test2) > 0 Then
Msgbox("test1 > test2","")
Else If test1.CompareTo(test2) < 0 Then
Msgbox("test1 < test2","")
Else
Msgbox("test1 = test2","")
End If

Many hours I spent because this little problem ...
Thanks to thedesolatesoul, margret, BarrySumpter, mc73 for help
 
Upvote 0

BarrySumpter

Active Member
Licensed User
Longtime User
Hey thanks, awama, for sticking with it and posting the solution.


If B4A designers/developers don't want to allow this type of > compile comparison
that is available in basic, cobol, vb6, RB, LC, .net, etc.
Who am I to say different.

Just nice to have a . solution.

:sign0060:

But still weird.
 
Last edited:
Upvote 0
Top