Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Counting character in a string

843785Feb 6 2009 — edited Feb 6 2009
I'm trying to count how many times a characters occurs in a string, and I'm having a lot of trouble. This is what I have so far:

note: the method call has to be public static int countOccs(char f, String s) and I know that it can be done using only loops, for, charAt, and length () and I want to learn it like that.
[I just chose f, s randomly.]

public class Counting {
public static void main(String [ ] args) {
String s = "Testing";
int countOccs = s.length( );
char [ ] charArray = new new char[counting];
for (int i = 0; i < counting; i++) {
charArray[i]
}
public static int counting(char f, String s) {
}
}

I'm really lost. When I try to define char f = "f" or String s = "Testing" in the method call (public static int counting...) I get messages saying that it has already been declared in the call, but there has been nothing assigned to it. Anyways, I feel really lost. I did find a pattern by looking at my notes from awhile ago, but am having trouble filling out parts of it. (Thanks, memory...) I think the problem comes from having to have the method call be public static int counting (char f, String s). That part confuses me a lot because I want it to print out how many characters of f are in a string, but it doesn't have "void" in it so it doesn't return a value.

for (int i=0; i < s.length(); i++)
{
char f = s.charAt(i);
if (something that looks at the characters)
{
do something like count
}
}

Edited by: MAresJonson on Feb 6, 2009 6:50 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 6 2009
Added on Feb 6 2009
8 comments
525 views