Results 1 to 5 of 5
  1. #1
    colpwd is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default Accessing array index through the result of a string concatenation

    Hi all,

    I was hoping someone could assist me with the following problem.

    I am fundamentally calling a method from another class through the use of an object. And this method has an input paramater which is a string.

    so like this,

    statusTest.value.setName(...), where ... is a string.

    Within the call, i am passing a string value through an index of an array,

    statusTest.value.setName(statDetails[0]);

    where statDetails is an array i have declared and initialised earlier.

    My problem is, I also have an array declared statDetailsCool that i want to use in the same fashion but only in certain circumstances.

    So i want to do something like the following,

    statusTest.value.setName("statsDetails".concat(ano therArray[0])[0]);

    where anotherArray has been declared and initalised and contains "Cool" in its first index.

    The compiler views this as a string usage rather than an array as it expects.

    Any ideas?

    Thanks.

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    I'm not totally sure I understand the question. Are you trying to pass two joined strings as a single string to a function? In that case, you're looking at a simple "a"+"b" format:
    Java Code:
    statusTest.value.setName(statDetails[0] + anotherArray[0]);
    If I misunderstand your question I apologize, but that seems like what you're looking for. If not, could you provide some sample variables & input you might expect to see?

  3. #3
    colpwd is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default

    Hi Zack,

    Thanks for your reply.

    I am trying to do something similar.

    I have an array called RegStatDetailsCool and i want to access its indexes.
    Normally i would just go, RegStatDetailsCool[0...n]

    But instead i want to concantenate the "RegStatDetails" part with "Cool"

    "RegStatDetails".concat("Cool")[0...n].

    In the end i want this to happen,

    String[] anotherArray = {"Cool", "Hot"};
    "RegStatDetails".concat(anotherArray[0...n])[0...n];

    Hope this makes it a bit clearer.

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Ah, yes, I see what you're getting at now. Unfortunately, this isn't possible by conventional methods.

    You have a few alternatives; instead of using a set of variables, you could use a hash map (so the keys would be something like RegStatDetails, RegStatDetailsHot, RegStatDetailsCool, and the values would be the arrays).
    HashMap (Java Platform SE 6)

    The other alternative, albeit horrendous, would be to use JavaScript to evaluate the expression. See here for an example: Java Programming - Evaluate String math expression [Locked]. I wouldn't recommend this method necessarily, but it's up to you.

  5. #5
    colpwd is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 12-17-2009, 04:06 PM
  2. Replies: 12
    Last Post: 11-05-2009, 08:12 AM
  3. Database Column vs String Concatenation
    By varun_33 in forum JDBC
    Replies: 1
    Last Post: 12-11-2007, 05:14 PM
  4. Accessing index.dat files
    By vissu007 in forum Networking
    Replies: 1
    Last Post: 07-01-2007, 05:47 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
  •