0

I'm using a for loop to append an "m" on to a string array, then passing each string to a DDE connection to download a stock quote.

    String[] symbols = {"AUDUSD", "EURUSD", "GBPUSD", "USDJPY"};

    String ibfxSym[] = new String[symbols.length];

    for(int i = 0; i<symbols.length;i++) {
        ibfxSym[i] = symbols[i] + "m";

    }
            // start DDE
    for (int i = 0; i < connections.length; i++) {
        try {
            connections[i].getQuote(ibfxSym[i]);
        } catch (Exception e) {
            System.out.println("error");
        }

That does not work, but if I pass in:

String[] ibfxSym = {"AUDUSDm", "EURUSDm", "GBPUSDm", "USDJPYm"};

instead of the loop it works just fine. I've tried printing each string as it is created in the loop, and they look the same. Is there some weird formatting the loop is adding that I can't see? I even tried:

ibfxSym[i] = String.valueOf(ibfxSym[i].trim());

to see if there was maybe a carriage return or something being appended that I couldn't see. Any ideas about what's going wrong here? Thanks.

4
  • 1
    are you saying that after the loop, the ibfxSym[] array doesn't contain what you thought it would? There shouldn't be any carriage returns being appended, and the code seems correct, except for the getQuote(ibfxSym[i]) - is the number of connections the same as the array size? Commented Jan 13, 2011 at 19:00
  • 1
    Are you sure connections.length is the same as ibfxSym.length? Commented Jan 13, 2011 at 19:00
  • 1
    "That does not work" could be more specific. Commented Jan 13, 2011 at 19:03
  • 2
    What do you see when you debug your program line by line? BTW: why do you discard any exception thrown, it may contain helpful information. Commented Jan 13, 2011 at 19:03

4 Answers 4

2

You're not reading from the same array that you're modifying...

    ibfxSym[i] = symbols[i] + "m";

for (int i = 0; i < connections.length; i++) {
    try {
        connections[i].getQuote(ibfxSym[i]);

In other words, you are assuming that i, being derived from iterating over connections, is also a valid index for ibfxSym.

Sign up to request clarification or add additional context in comments.

5 Comments

What? He's assigning to ibfxSym and calling getQuote on it as well.
@Kylar: The point @Falmarri is making is that the OP is creating ibfxSym and then iterating over it with an index derived from iterating over connections, which isn't safe unless you know they are the same length or if the former is smaller.
Ahh, that's not what he said. He's implying that he's printing from a different array, not a different index counter. I see what you're saying though.
The wording of this answer really confused me as well.
Actually @Kylar is right, I misread the code. But now the answer is that he didn't give us enough information.
2

Your loop is working just fine, the connection loop instead would work only if your connections array maps your ibfxSym array though..

Comments

1

if

connections.length

is bigger than

ibfxSym.length

or in this case 4, you should get an array index out of bounds exception i think.

Comments

0

Have you tried to do this:

for(int i = 0; i<symbols.length;i++) {
    ibfxSym[i] = new String(symbols[i] + "m");

}

4 Comments

Calling a String constructor directly is redundant here.
Using + creates a new String, having a new new String shouldn't make much difference.
This happens under the hood already (sorta.. a stringbuffer actually gets used in both situations.)
I thought so, but wasn't sure when it came to arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.