type casting in android (again) monkey v49

Monkey Targets Forums/Android/type casting in android (again) monkey v49

AdamRedwoods(Posted 2011) [#1]
I'm getting an error in Android again when I try to type cast a non-integer to integer. Was this ever resolved or is this going to be a hidden "gotcha" for monkey?

tempstr= s.Split(DELIM)
For s = Eachin tempstr
	If s.Length < 1 Then Continue
				
	Local sl:Int = Int(String.FromChar(s[0]))
	'...etc...
Next


Android output:
int t_sl=Integer.parseInt((String.valueOf((char)((int)t_s.charAt(0)))).trim());


The problem is what is the best way to check for integers only? And if I'm doing it manually for Android, wouldn't it be just be better for monkey to do it natively?

EDIT:
To fix, add
If s[0] < 48 Or s[0] > 57 Then Continue


EDIT:
Ok, I see why it's done like this. Integer.parseInt() is a built-in android function and throws exceptions on non-integer values.
Still, I hate hidden "gotchas". This could at least be in the docs.


Midimaster(Posted 2012) [#2]
sorry to re-animate this thread, but I have the same problems and the solution above is not perfect:

If the user entered not only numbers, but also alphabetic charcters or nothing, the methode...
[monkeycode]s$="14r" ' input string with wrong character
If s[0] < 48 Or s[0] > 57 Then Continue
Value=Int(s)[/monkeycode]
...fails.

Android also throws an exeption if one of the characters is not numeric.

So now I try it this way, but it there a better solution?
[monkeycode]s$="14r" ' input string with wrong character
If s="" Then Continue
for l=EachIn S
If l < 48 Or l > 57 Then
Cont=TRUE
Endif
If Cont=TRUE then Continue
Value=Int(s)[/monkeycode]


Gerry Quinn(Posted 2012) [#3]
Well, it does kind of say it in the docs: if you look up explicit conversions between types, it says that String to Int is target-dependent.

I guess if you want a standard version you need to write your own.


Midimaster(Posted 2012) [#4]
Thank you,

I thought, Monkey would handle this in "old style" BlitzMax manner and looks only on leading numeric characters. I was used that "15r" returnes 15.


Jesse(Posted 2012) [#5]
I don't know if this is useful to anybody but this resolves for negative values and will only read the negative sign plus the first ten digits:
[monkeycode]

Function ToInt:Int(s:String)
Local n:Int =0
Local sgn:Int = 1
If s.Length() = 0 Return 0
If (s[0] < 48 Or s[0] > 57) And s[0] <> 45 Return 0
Local len:int = s.Length()
If s[0] = "-"[0] len = 11 Else len = 10
If s.Length() > len len = 10
If s.Length() < len len = s.Length()
For Local i:Int = 0 Until len
If i = 0
If s[i] = 45
sgn = -1
Else
n = s[i] - 48
endif
Elseif s[i] >=48 And s[i]<= 57
n = n * 10 + (s[i]-48)
Else
Exit
Endif
Next
Return (n*sgn)
End Function
[/monkeycode]