Avoid Using indexOf
Checks for Positive Numbers in Java
What is it?
This practice is triggered when checks against an indexOf
value compare it with any positive number, which can lead to incorrect behavior by potentially ignoring valid positions, specifically the first element.
Why apply it?
Using indexOf
checks for positive numbers (e.g., > 0
) likely indicates a bug because index 0 is valid. This kind of check can skip verifying the presence of an element at the start of a string or list. If the goal is to check presence, use contains
instead. For skipping the first element, consider >= 1
.
How to fix it?
Replace checks with proper intent: use >= 1
if deliberately skipping the first element, or use contains
for existence checks.
Examples
Example 1:
Negative
The negative example uses indexOf
with a > 0
condition, which incorrectly ignores matches at the start of the string.
String name = "moby dick";
if (name.indexOf("moby") > 0) { /* Noncompliant */
System.out.println("The string contains 'moby'.");
} else {
System.out.println("'moby' not found.");
}
Example 2:
Positive
The positive example uses the contains
method for checking the presence of a substring, making the intention clear and avoiding pitfalls of index comparison.
String name = "moby dick";
if (name.contains("moby")) { /* Compliant */
System.out.println("The string contains 'moby'.");
} else {
System.out.println("'moby' not found.");
}
Negative
The negative example uses indexOf
with a > 0
condition, potentially not finding a valid substring starting from the first character.
String phrase = "hello world";
if (phrase.indexOf("hello") > 0) { /* Noncompliant */
System.out.println("'hello' found after the first character.");
} else {
System.out.println("'hello' not encountered after the first character.");
}
Example 3:
Positive
This positive example uses a check for >= 1
to deliberately skip the first element when searching for a substring.
String phrase = "hello world";
if (phrase.indexOf("world") >= 1) { /* Compliant: intentionally ignoring first character */
System.out.println("'world' found after the first character.");
} else {
System.out.println("'world' not encountered after the first character.");
}