The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Not sure what is wrong with my code.

Hi all,
I tried to make a program that takes a string str, and char a and checks how many times the char is used in the string.
Example: the string Welcome and the letter e, is 2 times. so the program should print 2.
It compiles but when I run it and enter the information, i cannot get the printing line out.
Thanks!!

Heres my code:

importjava.util.Scanner;class program
{publicstaticvoid main(String[] args){
Scanner user_input=new Scanner(System.in);String str;String b;System.out.print("Please enter a word");
str=user_input.next();System.out.print("Enter the charachter you would like to check for");
b=user_input.next();char a=b.charAt(0);int result;
result=count(str,a);System.out.println(" The number of times the letter "+a+" has been used in the word "+str+" is "+result);}publicstaticint count(String str, char a){int counter=0;int i=0;while(str.length()>i){while(str.charAt(i)==a){
counter++;
i++;}
str.substring(counter);}return counter;}}