Android Question Erel_Collection_Example

Shelby

Well-Known Member
Licensed User
Longtime User
Going through Erel's tutorial: Erel_Collection_Example following with my B4A program I find that an error message occurs saying that the word wordsCount should be changed to wordsLength. However when I replace all the occurences I still have a hangup. The next error message is: 'For each word in Words.Length.Keys Word:Length' I'm not sure how to perform a work around since I'm using B4A and Erel is using B4J. Thanks
 

Shelby

Well-Known Member
Licensed User
Longtime User
Please let me know if this is not permitted due to some rules I'm not aware of. Do I always use an apostrophe like this?

'
Sub Process_Globals
End Sub
Sub Globals
End Sub
Sub Activity_Create(FirstTime As Boolean)
CountWords(File.ReadString(File.DirAssets, "pg20795.txt"))
End Sub

Sub CountWords(Text As String)
Dim Words() As String = Regex.Split($"[\s,.()!?"\-]"$, Text)
Dim wordsLength As Map
wordsLength.Initialize
For Each word In Words
Dim count As Int
If wordsLength.ContainsKey(word) Then
count = wordsLength.Get(word)
count = count + 1
Else
count = 1

End If
wordsLength.Put(word, count)
Next

For Each word In Words.Length.Keys
Log($"${word}: {wordsLength.Get(word)}"$)
Next
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub '

Thanks
It's from the 5 minute 36 second point on your tutorial.
 
Last edited:
Upvote 0

Shelby

Well-Known Member
Licensed User
Longtime User
I'm still in the same tutorial (Erel_Collections_Example) and I get the following error message when I run debug.
Are you missing a library reference? Error occurred on line: 21
word = word.ToLowerCase
Word: tolowercase

'In Erel's code it's also line 21'


I clicked each library separately in the Library Manager and checked to see if the 'word.ToLowerCase' would function properly but no dice. My question? Where do I find a library which will solve this dilemma? Thanks

Here's my code:
B4X:
Sub CountWords(Text As String)
        Dim words() As String = Regex.Split($"[\s,.()!?"\-:/\\;]+"$, Text)
        Dim wordsLength As Map
        wordsLength.Initialize
        For Each word In words
            Dim count As Int
'line 21' word = word.ToLowerCase
            If wordsLength.ContainsKey(word) Then
                count = wordsLength.Get(word)
                count = count + 1      
            Else
                count = 1
      
            End If
            wordsLength.Put(word, count)
        Next
        Dim list1 As List
        list1.Initialize
        For Each word In wordsLength.Keys
            Dim wc As WordCount
            wc.Initialize
            wc.Word = word
            wc.Count = wordsLength.Get(word)
            list1.Add(wc)
        Next
 
        list1.SortTypeCaseInsensitive("Word", True)
        For Each wc As WordCount In list1
            Log($"${wc.Word}: ${wc.Count}"$)
        Next
End Sub
 
Last edited:
Upvote 0

Jorge M A

Well-Known Member
Licensed User
Longtime User
Please use [CODE ][ /CODE] tags (without spaces) between your code.

ToLowerCase is a Core library method.

Change your For Each by this:

B4X:
Dim l As Int=words.Length
    For x = 0 To l -1
        Dim count As Int
        words(x) = words(x).ToLowerCase

        (rest of your code)

    Next
 
Upvote 0

Shelby

Well-Known Member
Licensed User
Longtime User
Thank you Jorge. Can a person view the contents of the libraries in the Libraries Manager tab? If so, how do I view a library?
 
Last edited:
Upvote 0
Top