Int array?

Kevin

Well-Known Member
Licensed User
Longtime User
I am trying to create an array of integers but am not quite sure how to do that.

Original Java code:
B4X:
int[] mod1 = {343,171,22,20,22,62,22,62,22,62,22,62,22,62,22,20,22,62,22,20,22,62,22,62,22,62,22,62,22,62,22,20,22,62,22,62,22,20,22,20,22,20,22,20,22,62,22,20,22,20,22,20,22,62,22,62,22,62,22,62,22,20,22,62,22,62,22,1360,343,83,22,3806};

So it looks like it is assigning an array of numbers to mod1.

Other than how to show this as an int array in B4A, I also would like to add this array to a type, but I don't think I did that right...

B4X:
Type MyData (a as Int, b as Int, mod1 as Array As Int)


Any ideas?
 

Kevin

Well-Known Member
Licensed User
Longtime User
I'm receiving an error when running. I think I know WHY, but I don't know how to fix it.

The source of the integers for the array are stored in a string and I am trying to assign those numbers in the string (separated by commas) to the Int array. But I get the following error:

(NumberFormatException) java.lang.NumberFormatException: Invalid double: "343,171,22,22,22,22,22,22,22,22,22,22,22,22,22,64,22,22,22,64,22,64,22,64,22,64,22,64,22,64,22,22,22,64,22,22,22,64,22,22,22,22,22,64,22,22,22,22,22,22,22,64,22,22,22,64,22,64,22,22,22,64,22,64,22,64,22,1520,343,85,22,3666"

Below is the line causing it. Incidentally, the value of the string is as shown in the error line above. (sCMD = "343,171,22,.........")

B4X:
Dim Mod1() As Int = Array As Int (sCMD)

How can I convert the numbers in the string to an array? :BangHead:
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
That syntax is not valid as the compiler needs to know the values as literals at compile time.

B4X:
Dim s, sa() As String
s = "343,171,22,22,22,22,22,22,22,22,22,22,22,22,22,64,22,22,22,64,22,64,22,64,22,64,22,64,22,64,22,22,22,64,22,22,22,64,22,22,22,22,22,64,22,22,22,22,22, 22,22,64,22,22,22,64,22,64,22,22,22,64,22,64,22,64 ,22,1520,343,85,22,3666" 
sa = Regex.Split(",", s)
Dim ia(sa.Length) As Int
For i = 0 To sa.Length - 1
   ia(i) = sa(i)
Next
 
Upvote 0
Top