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!

Letter Counter

807599Nov 28 2006 — edited Nov 29 2006
I am trying to write a program that asks the user to enter a string and then asks the user to enter a character. The program is supposed to count and display the number of times that the specified character appears in the string.

So far my code is:
import java.util.Scanner;    // Needed for the Scanner class

public class LetterCounter
{
   public static void main(String[] args)
   {
   	
   	// Create a Scanner object for keyboard input.
    Scanner keyboard = new Scanner(System.in);

    String string;
    char character;
	
    System.out.print("Please enter a string:  ");
    string = keyboard.nextLine();
      
     
    System.out.print("Please enter a character:  ");
    character = keyboard.nextchar();
    
    int letter = string.charAt(character);

    
System.out.println ("The character " + character + 
				   "appears in your sting" + letter +
                                   " times.");


   }
}
The error says "Cannot find symbol method nextchar()"

I also am unsure of how to count the times a character appears in a string.

Any help would be highly appreciated.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 27 2006
Added on Nov 28 2006
12 comments
134 views