Android Question declared value

Discussion in 'Android Questions' started by Almora, Apr 26, 2019.

  1. Almora

    Almora Active Member Licensed User

    I used this code to give a lot of value, but it gives an error. I think I'm doing it wrong. How do I fix this? Is there a practical way?

    Code:
    Dim ka(160As Int


        ka(
    0)="www.a..."
        ka(
    1)="www.b..."
        ka(
    2)="www.c..."
        .....
        ka(
    160)="www.xx..."

       

        WebView1.LoadHtml(ka(
    0))
        WebView1.LoadHtml(ka(
    1))
        WebView1.LoadHtml(ka(
    2))
        ....
        WebView1.LoadHtml(ka(
    160))
     
  2. José J. Aguilar

    José J. Aguilar Well-Known Member Licensed User

    What error do you get?
    Don't should you declare the array as string?
    Max value should be 159¿, arrays index start from 0
     
    emexes and DonManfred like this.
  3. DonManfred

    DonManfred Expert Licensed User

    Yes? I don´t see any errormessage in your post!
    You can not load multiple urls at once.

    Away from that
    You are creating an array of INTEGERS and fill them with STRINGS?
     
  4. Almora

    Almora Active Member Licensed User

    Code:
    Dim ka(160As String

    Dim WebView1 as WebView
    Dim WebView2 as WebView
    Dim WebView3 as WebView



        ka(
    0)="www.a..."
        ka(
    1)="www.b..."
        ka(
    2)="www.c..."
        .....
        ka(
    160)="www.xx..."


      
    if Main1.a=0 Then
        WebView1.LoadHtml(ka(
    0))
        WebView2.LoadHtml(ka(
    1))
        WebView3.LoadHtml(ka(
    2))

     
    Else If Main1.a=1 Then
        WebView1.LoadHtml(ka(
    3))
        WebView2.LoadHtml(ka(
    4))
        WebView3.LoadHtml(ka(
    5))

    Else If Main1.a=2 Then
        WebView1.LoadHtml(ka(
    6))
        WebView2.LoadHtml(ka(
    7))
        WebView3.LoadHtml(ka(
    8))

    ...........



    End If
    error code:
    java.lang.ArrayIndexOutOfBoundsException: length=160; index=160
     
  5. DonManfred

    DonManfred Expert Licensed User

    You are NOT reading (or not understanding) the answers given here!
    This is BASIC KNOWLEDGE about b4x language!
    start with reading the BEGINNERS GUIDE or watching Erels Videos. It is all explained there.
     
    MarkusR and Star-Dust like this.
  6. Star-Dust

    Star-Dust Expert Licensed User

    :D:D
     
    Last edited: Apr 26, 2019
    DonManfred likes this.
  7. DonManfred

    DonManfred Expert Licensed User

    Star-Dust likes this.
  8. Almora

    Almora Active Member Licensed User

    I would go to the problem of research.
    this work..


    Code:
    Dim ka(161as String

    For i =0 To ka.Lenght-1
    ...
    Next
    ...
    Don..:cool:
     
    Last edited: Apr 26, 2019
  9. Kiffi

    Kiffi Well-Known Member Licensed User

    I doubt that. :rolleyes:
     
  10. Almora

    Almora Active Member Licensed User

    you can be sure..:D
     
  11. Almora

    Almora Active Member Licensed User

    I think this is a better solution ..

    Code:
    Dim ka() As String = Array As String( _
        
    "", _
        
    "website 1", _
        
    "website 2" _
    )

    Dim NumWebsites As Int = ka.Length - 1    '1-based array, numbered 1..NumWebSites

    For I = 1 To NumWebsites
        WebView1.LoadHtml(ka(I))
    Next
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    Code:
    For Each value As String in ka
     WebView1.LoadHtml(value)
    Next
     
    trueboss323 likes this.
  13. Computersmith64

    Computersmith64 Well-Known Member Licensed User

  14. emexes

    emexes Well-Known Member Licensed User

    I think that table was compiled by someone with a bias towards 0, rather than no bias. It looks like there is a battle about whether arrays should start at 0 or 1, and the combatants have completely missed the question of: why even have this constraint at all?

    Every BASIC compiler I've used since VAX BASIC in the '80s has allowed things like Dim StudentsInYearLevel(7 to 12) or SuperBowlChampion(1967 to CurrentYear). Pascal too, and that language isn't even on the list (wtf?). And I'm pretty sure FORTRAN did too, probably even earlier.

    Better yet, indices can be negative, eg: Dim DirectionNSEW(-1 to 1, -1 to 1) Pascal could even apply range-checking to integer (maybe even scalar) variables, thus you could define Temperature as being -273..101, or EggsInCarton as 0..12, or DiceTotal as 2..12.

    But... but... but... 0-based arrays are more efficient. Agreed. But Java is doing runtime bounds checks on array accesses anyway, so clearly factors other than efficiency were considered. Like making the programmer's life easier :) So if we're already spending CPU cycles on calculating and checking array indexes, the cost of an extra subtraction is negligible in comparison to the benefit of array bounds that match the problem being solved rather than conforming to theoretical constraints.

    Better yet, you could have your cake and eat it too, eg, per PowerBASIC documentation:
    The whole discussion is a bit moot with regards to B4A, though, since it's built upon Java, interacts with Java libraries, using Java arrays, so: 0-based it is. At least we have the freedom to emulate 1-based natural-numbered arrays by dimming one more than we need and then ignoring element zero. All is good, as long as you don't post the code in public...

    ;-)
     
  15. Computersmith64

    Computersmith64 Well-Known Member Licensed User

    If you want to post code that uses non-0 based arrays that's up to you, but in this case the OP didn't seem to understand the implications of a 0-based array & others were trying to point out where he was going wrong.

    I see a lot of code posted that (in my mind) is not written "properly", but generally members of the forum limit their responses to addressing the issue rather than making comments on how the code is written. The few exceptions I've seen to this have been when the code is so bad that even if the primary issue is resolved, it still won't work as expected. I used to be nervous about posting my own code in public, but the reality is that there is always more than 1 way to skin a cat (a prime example being looping through array elements :)), so as long as a piece of code achieves the desired result you can't really say that it's "wrong". (just my 2c worth)

    Back on-topic (sort of): I think the best explanation I've seen on why 0-based arrays are 0-based is that index isn't really an index - it's an offset from the first element of the array, therefore the first element offset is 0 & the last element offset is array size - 1.

    - Colin.
     
  16. emexes

    emexes Well-Known Member Licensed User

    :) great minds think alike

    For one dimension, agreed.

    edit: remainder of post deleted (subsumed by cat-skinning idiom ;-)
     
    Last edited: May 2, 2019
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice