Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone, I am using String.indexOf() to find all occurrences of a character in a string using a for loop. The problem is, I taught that if I iterate over the string in a for loop till the end of the length of the string the iteration should end and print only the occurrences of that character that was found but the for loop still prints -1, meaning that it didn't stop after finding the last occurrence of the character.

What I have tried:

public static void main(String[] args) {

     String str1 = "hello from lagos";
	   String guess = "l";
	   int index = str1.indexOf(guess);
	   System.out.println("index is : " + index); // to show where the first occurrence is 
	   for(int i = 0; i < str1.length(); i++) {
		   System.out.println("For many ocuurrences: " + index);
		   index = str1.indexOf(guess, index + 1);
		      
	   }
	}


Output:

index is : 2
For many ocuurrences: 2
For many ocuurrences: 3
For many ocuurrences: 11
For many ocuurrences: -1
For many ocuurrences: 2
For many ocuurrences: 3
For many ocuurrences: 11
For many ocuurrences: -1
For many ocuurrences: 2
For many ocuurrences: 3
For many ocuurrences: 11
For many ocuurrences: -1
For many ocuurrences: 2
For many ocuurrences: 3
For many ocuurrences: 11
For many ocuurrences: -1


My question is: why didn't the loop stop after printing the first 2, 3 and 11. Why is it printing -1 and why didn't the loop stop after the printing the first 11 ? Thanks for any help.
Posted
Comments
Richard MacCutchan 18-Dec-23 6:56am    
You are repeating the same code for every character position in the string, which is incorrect. You should just use a simple do or while loop that terminates as soon as the search returns -1, which indicates no more matches.
UT7 18-Dec-23 19:41pm    
@Richard MacCutchan, thanks a lot. I already have a while loop solution but I wanted to see if a for loop can also work. Thanks a lot.

1 solution

This is where running the code under the debugger and stepping through the code line by line and inspecting variables would have helped you solve this faster than you could have typed the question into CodeProject.

The problem is not about indexOf. The problem is your management of the loop.

You shouldn't be using a for loop for this. A while loop would be more appropriate, and you only need one variable to track the position of the occurrence, not two.
 
Share this answer
 
v2
Comments
UT7 18-Dec-23 19:36pm    
@Dave Kreskowiak, thanks a lot. I already have a while loop solution but I wanted to see if it can also work with a for loop.
String str = "hello from lagos";
String guess = "l";
int index = str.indexOf(guess);
while(index >= 0) {
System.out.println(index);
index = str.indexOf(guess, index + 1);
}
Thanks a lot.
Dave Kreskowiak 18-Dec-23 19:53pm    
A for loop isn't appropriate for use with indexOf because the index of the next occurance will never reach the end of the loop. The loop will never end and if you did use another variable to manage the loop, you'd be calling indexOf repeatedly with either the same results over and over again or calling it unnecessarily with no results. Using a for loop in this situation is just inefficient.
UT7 18-Dec-23 19:55pm    
Okay, I get it now, thanks a lot.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900