counting letters in a string?

public int countLetter(char let){

int index = 0;

int count = 0;

while (index<string.length())

if (string.charAt(index)==let){

count++;

}

index++;

return count;

}

the goal of this part of the program is to have the user input a string (string) and a char (let) and then count the number of times that character appears in the string. i'm trying to make it case insensitive. i thought about using equalsIgnoreCase or toUpperCase but i can't change 'let' to a string. any ideas? thx>

[557 byte] By [obeserabbita] at [2007-11-26 19:05:44]
# 1
Look in the Character class. You will find some handy methods: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Character.html
DrLaszloJamfa at 2007-7-9 20:56:17 > top of Java-index,Java Essentials,New To Java...
# 2
Try creating two new variables, the lowercase version of the letter and the string.
floundera at 2007-7-9 20:56:17 > top of Java-index,Java Essentials,New To Java...
# 3

ok i tried implementing some of what you guys said, do you think this will work?

public int countLetter(char let){

int index = 0;

char c = Character.toUpperCase(let);

int count = 0;

while (index<string.length())

if (string.charAt(index)==let||string.charAt(index)==c){

count++;

}

index++;

return count;

}

>

obeserabbita at 2007-7-9 20:56:17 > top of Java-index,Java Essentials,New To Java...
# 4
Have you tried it?
DrLaszloJamfa at 2007-7-9 20:56:17 > top of Java-index,Java Essentials,New To Java...
# 5
yes, i tried running it against a test, but it seems to freeze up.
obeserabbita at 2007-7-9 20:56:17 > top of Java-index,Java Essentials,New To Java...
# 6
Your increment:index++; is *after* the loop, so the loop spins infinitely. I would suggest using a for loop, too.
DrLaszloJamfa at 2007-7-9 20:56:17 > top of Java-index,Java Essentials,New To Java...
# 7
If let is already uppercase then the comparison to c in the if statement is redundant.
floundera at 2007-7-9 20:56:17 > top of Java-index,Java Essentials,New To Java...
# 8

flounder, in the test, it tests for a lowercase letter, so i think its ok right?

also, i used a for loop and now my test succeeds in all but one area.

if the user types in Building and wants to know how many u's there are, it returns zero instead of one.

public int countLetter(char let){

char c = Character.toUpperCase(let);

int count = 0;

for(int index=0; string.charAt(index)==let||string.charAt(index)==c&&index<string.length(); index++)

count++;

return count;

}

>

obeserabbita at 2007-7-9 20:56:18 > top of Java-index,Java Essentials,New To Java...
# 9
I'd say it would be failing on a lot more examples than that one. Why not try writing a normal for loop and go back to using the if statement.
floundera at 2007-7-9 20:56:18 > top of Java-index,Java Essentials,New To Java...
# 10

awesome, thanks for the suggestion

i tested this and it works :)

public int countLetter(char let){

char c = Character.toUpperCase(let);

int count = 0;

for(int index=0;index<string.length(); index++)

if (string.charAt(index)==let||string.charAt(index)==c)

count++;

return count;

}

>

obeserabbita at 2007-7-9 20:56:18 > top of Java-index,Java Essentials,New To Java...
# 11
Do you understand why your previous attempt didn't work?
floundera at 2007-7-9 20:56:18 > top of Java-index,Java Essentials,New To Java...
# 12
yes, i think so
obeserabbita at 2007-7-9 20:56:18 > top of Java-index,Java Essentials,New To Java...
# 13
Either you do or you don't. Try explaining it to me. If you can, then you do.
floundera at 2007-7-9 20:56:18 > top of Java-index,Java Essentials,New To Java...