0

I need to find the first match in this task. Probably i am just missing something. As you can see, i found the last match.I am not copied the first half of the code. Thank you.

for (int i = 0; i <= n - 1; i++) {
    if (iv[i] == a) {
        hely = i;
    }
}
if (hely == -1) {
    System.out.println("text");
} else {
    System.out.println("text " + a + " text " + (hely + 1) + "text");
}
2
  • Too ambiguous. Try to give some expected input and output. Commented Mar 12, 2016 at 13:05
  • You just need to exit out of your loop when you find a first match, no need to continue looping after first match. Commented Mar 12, 2016 at 13:16

2 Answers 2

2

break the loop when you find first match.

for (int i = 0; i <= n - 1; i++) {
    if (iv[i] == a) {
        hely = i;
        break;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

You need to exit the for loop after finding the first match:

for (int i = 0; i <= n - 1; i++) {
    if (iv[i] == a) {
        hely = i;
        break;
    }
}

Comments

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.