Orhun Seyran
Orhun Seyran

Reputation: 13

Unclear on use of "fromIndex" in "indexOf(int ch, int fromIndex)"

I'm trying to use indexOf method in the second line written below, to do that as you know you need to put an integer after coma, but in my case I don't want to put an actual integer, I want to put an integer variable. (guessedLetter variable is a String variable that I have defined previously)

int aaa=bookTitle.indexOf(guessedLetter);
int bbb=bookTitle.indexOf(guessedLetter,aaa);

If I type the code in this way, It doesn't function properly. But if I type the code like written below;

int bbb=bookTitle.indexOf(guessedLetter,5);

it works. So you need to use an actual integer, but as I said I need to use an integer variable instead.

Is there a way to do that?

Thank you for your time.

Upvotes: 1

Views: 75

Answers (1)

p-a-o-l-o
p-a-o-l-o

Reputation: 10057

Given that the char is found at aaa position, to find more occurrences of it you have to increment the starting index at least by one:

int bbb=bookTitle.indexOf(guessedLetter,aaa + 1);

otherwise you go on finding the same char in the same position.

Upvotes: 1

Related Questions