Results 1 to 4 of 4
  1. #1
    dpedroia15 is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default Passing an Array index into a function?

    Hey, I'm trying to make a simple function that returns a value according to what was chosen by the user in a text field.

    So it's something like this:

    Java Code:
    float getModifier(JList list[])
    {
        int selected = list[?].getSelectecValue( ); //Obviously gives a compiler error, don't know how to pass the index into the function correctly
        switch(selected)
              case 1:
                  return blahblahblah
    
           //etc...etc..
    }
    Thanks for any help, in advance...

  2. #2
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    5

    Default

    so somewhere you call
    .... object.getModifier(list[])

    than you receive it in the getModifier method, where you would put a value where you have the ? and you shouldn't need to call getSelectecValue() (getSelectedValue), unless the array is holding objects.

    hm.
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,259
    Rep Power
    25

    Default

    Couple of suggestions:
    Do the array de-referencing before or in the call to the function and pass a JList reference.
    ...object.getModifier(list[ix])
    Pass the needed index as an int parameter to the function.
    ...object.getModifier(list[], ix)

  4. #4
    dpedroia15 is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default

    Thanks for the replies.

    Norm, I will try your answer, looks like it should work.

    Thanks again guys :)

Similar Threads

  1. Example for passing string to a function.?
    By mlibot in forum New To Java
    Replies: 1
    Last Post: 03-19-2010, 06:36 AM
  2. Array Index out of bounds
    By leapinlizard in forum New To Java
    Replies: 5
    Last Post: 04-29-2009, 05:11 AM
  3. how use string array while passing to a function
    By sks9s9 in forum New To Java
    Replies: 2
    Last Post: 02-03-2009, 12:44 PM
  4. Passing a Vector object to a function
    By evapisces in forum New To Java
    Replies: 4
    Last Post: 09-27-2008, 03:18 AM
  5. problems with array index
    By mary in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 04:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •