Newbie Java - IndexOf() problem

J

john

Hi, I'm a total idiot newbie, can anyone please help!

I am trying to use the indexof() function to find out the number of
occurences of a single specified character in a user defined string.
Any assistance would be greatly appreciated as I can't work it out.

John
 
C

Christophe Vanfleteren

john said:
Hi, I'm a total idiot newbie, can anyone please help!

I am trying to use the indexof() function to find out the number of
occurences of a single specified character in a user defined string.
Any assistance would be greatly appreciated as I can't work it out.

John

String s= "this has two a's in it";
char toFind = 'a';
int index = s.indexOf(toFind);
int count = 0;
while (index != -1) {
count ++;
index = s.indexOf(toFind,index+1);
}
System.out.println("the char "+toFind+" was found "+count+" times.");
 
M

Marco Schmidt

john:
I am trying to use the indexof() function to find out the number of
occurences of a single specified character in a user defined string.
Any assistance would be greatly appreciated as I can't work it out.

You have to use the version of indexOf that takes an int offset as a
second parameter and run in a loop counting occurrences. Something
like that:

public static int countOccurrences(String s, char c) {
int index = 0;
int found = 0;
do {
index = s.indexOf(c, index);
if (index != -1) {
found ++;
}
} while (index != -1);
return found;
}

Regards,
Marco
 
R

Roedy Green

I am trying to use the indexof() function to find out the number of
occurences of a single specified character in a user defined string.

It would be simpler just to loop using charAt and testing yourself for
the desired character. Using indexOf would be much more complicated.
 
S

Sudsy

john said:
Hi, I'm a total idiot newbie, can anyone please help!

I am trying to use the indexof() function to find out the number of
occurences of a single specified character in a user defined string.
Any assistance would be greatly appreciated as I can't work it out.

John

int count = 0;
int index = 0;
String s = "...";
char c = 'x';
while( ( index = s.indexOf( c, index ) ) >= 0 ) {
count++;
index++;
}

Set the s and c variables appropriately.
 
R

Roedy Green

while( ( index = s.indexOf( c, index ) ) >= 0 ) {
count++;
index++;
}

does that not have the potential to throw an subscript out of bounds
exception, or is indexOf safe for one past the end?
 
S

Sudsy

Roedy said:
does that not have the potential to throw an subscript out of bounds
exception, or is indexOf safe for one past the end?

Roedy,
Would I throw faulty code up here? The jackals would be all over me!
Here's what the javadocs have to say:

"There is no restriction on the value of fromIndex. If it is negative,
it has the same effect as if it were zero: this entire string may be
searched. If it is greater than the length of this string, it has the
same effect as if it were equal to the length of this string: -1 is
returned."

ps. I specifically tested in with edge conditions, the ones which
tend to come back and bite you in the a**.
 
R

Roedy Green

Would I throw faulty code up here? The jackals would be all over me!
I expected as much from you, but I wanted the peanut gallery to
realize that sort of thing does not necessarily come out in the wash
without checking.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top