Android Question String cannot be cast to ...

FERNANDO SILVEIRA

Active Member
Licensed User
Hello Guys,

I starded facing the following problem. Can anyone identify what am I doing wrong?

Error occurred on line: 53 (Main)
String cannot be cast to ...

Line presenting the error:
B4X:
  wWordEmpty.SubString2(0, wWordOK.Length - 1)     ' <=== ERROR LINE 53


B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Main")
    Log("*** Lendo tabela inteira para lista ***")
    lstWords1 = File.ReadList(File.DirAssets, "tabpalavras.txt")
    lstWords2.Initialize
    lstWords2.AddAll(lstWords1)
    lstWords2=ShuffleList(lstWords2)
    Log("LIST2  after shuffle: " &lstWords2)
   
    Dim wLeft As Int
    Dim cd As ColorDrawable
    Dim wLetras As Int
    Dim bg As Bitmap

    wWordOK = lstWords2.Get(Rnd(0,lstWords2.Size - 1))
    wWordOK = wWordOK.trim
    wWordEmpty = "          "
    wWordEmpty.SubString2(0, wWordOK.Length - 1)     ' <=== ERROR LINE 53
    wWordShuffled = wWordOK
   
    Do While wWordShuffled = wWordOK
        wWordShuffled = ShuffleWord(wWordOK)
    Loop
   
    bg = LoadBitmapResize(File.DirAssets, wWordOK & ".jpg", ImageView1.Width, ImageView1.Height, True)
    ImageView1.SetBackgroundImage(bg).Gravity = Gravity.CENTER
   
    wLetras = wWordShuffled.Length
    wLeft = (100%y - (wLetras * 45dip)) / (wLetras + 2)
    cd.Initialize2(Colors.Black, 5dip, 3dip, Colors.Green)
    DrawWord(wWordEmpty, wLeft, 250dip, True)
   
    wLeft = (100%y - (wLetras * 45dip)) / (wLetras + 2)
    cd.Initialize2(Colors.Red, 5dip, 3dip, Colors.Yellow)
    DrawWord(wWordShuffled, wLeft, 400dip, False)
   
End Sub

Regards,
Fernando
 

Attachments

  • APP04 JogoDasPalavras.zip
    143.9 KB · Views: 215

OliverA

Expert
Licensed User
Longtime User
Are you sure you did not mean to say
B4X:
wWordEmpty = wWordEmpty.SubString2(0, wWordOK.Length - 1)
SubString does not modify the existing string (Strings are immutable, more here: https://www.javatpoint.com/immutable-string), but returns a string that has a modified version of the original string.
On the other hand, that's interesting that a String.SubString2 method throws a class exception if it returns to nothing (the return is not captured by a variable). A B4A project from scratch does not have the same behavior. Looks like this only happens in Debug mode (for your attached project).
BTW, this line
B4X:
       lblLetter(i).Text = word.SubString2(i, i+1)
in your DrawWord sub will be your next challenge.
 
Upvote 0

FERNANDO SILVEIRA

Active Member
Licensed User
Thank you, Oliver

As I can see I still have a long way to go understanding substring concept within B4A.

To fix the issue I replaced the bugged line below:
B4X:
    wWordEmpty.SubString2(0, wWordOK.Length - 1)     ' <=== ERROR LINE 53

for this one and now it is working:
B4X:
     wWordEmpty = RepeatString(" ", wWordOK.Length - 1)


Sub RepeatString(str As String, times As Int) As String
    Dim result As String = ""
    For i = 0 To times
        result = result & str
    Next
    Return result
End Sub
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Even though for your code the performance gain may be near null, you may want to look into/learn about StringBuilder (https://www.b4x.com/android/help/core.html#stringbuilder). With it your code would become:
B4X:
Sub RepeatString(str As String, times As Int) As String
    Dim sb as StringBuilder
    sb.Initialize
    For i = 0 To times
        sb.Append(" ")
    Next
    Return sb.ToString
End Sub
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
you may want to look into/learn about StringBuilder
I think your code should be this way:
B4X:
Sub RepeatString(str As String, times As Int) As String
    Dim sb As StringBuilder
    sb.Initialize
    For i = 0 To times -1
        sb.Append(str)
    Next
    Return sb.ToString
End Sub
Log(RepeatString("OliverA", 6) )  'displays:  OliverAOliverAOliverAOliverAOliverAOliverA
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0
Top