CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2010
    Posts
    2

    Letter Count Program

    I'm writing a program which prompts the user to enter a sentence and then prompt the user to enter a single letter of the alphabet (into a char value). It's also supposed to count and display the number of words containing the letter entered (not case sensitive) and the total number of occurrences of the letter entered (not case sensitive). An example if the user entered the sentence: 'She sells seashells by the seashore.' and the letter 'S', the output from the program would be:
    4 words contain the letters S.
    8 S are found in the sentence.

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Letter Count Program

    Great - keep us informed of your progress, and if you get stuck on something, don't be afraid to ask.

    Teachers open the door, but you must enter by yourself...
    Chinese proverb
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Mar 2010
    Posts
    2

    Re: Letter Count Program

    import java.util.Scanner;

    public class CountSent
    {
    public static void main(String[]args)
    {
    Scanner Scan = new Scanner(System.in);

    int charCount = 0;

    System.out.println("\n\nThis program asks you to type in a sentence,");
    System.out.println("it then requires a single letter of the alphabet");
    System.out.println("to be entered in and displays the number of words");
    System.out.println("as well as the total number of occurrences.");

    System.out.print("Enter a sentence: ");
    String userString = Scan.nextLine();

    System.out.print("Enter a letter: ");
    char userChar = Scan.nextLine().charAt(0);


    userString = userString.substring(0, userString.length()-1);
    for (int i = 0; i < userString.length(); i++)
    {
    if (userString.charAt(i) == userChar)
    {
    charCount++;
    }
    }

    System.out.println( numWords + " words contain the letter "+ userChar +".");
    System.out.println( charCount + " " + userChar + " are found in the sentence.");

    }
    }

    Now the output is leaning towards the direction I want it, however it prints:

    words contains the letter S.
    8 S are found in the sentence.

    Rather than the example:

    4 words contain the letters S.
    8 S are found in the sentence.

    It won't print how many words contain the letter entered by the user. What can I do to make it so it matches the example?

  4. #4
    Join Date
    Oct 2008
    Posts
    77

    Re: Letter Count Program

    [CODE] tag, please

    numWords is not defined

  5. #5
    Join Date
    Mar 2010
    Posts
    2

    Re: Letter Count Program

    Code:
    import java.util.Scanner;
    
    public class CountSent
    {
    public static void main(String[]args)
    {
    Scanner Scan = new Scanner(System.in);
    
    int charCount = 0;
    int numWords = 0;
    
    System.out.println("\n\nThis program asks you to type in a sentence,"); 
    System.out.println("it then requires a single letter of the alphabet");
    System.out.println("to be entered in and displays the number of words"); 
    System.out.println("as well as the total number of occurrences.");
    
    System.out.print("Enter a sentence: ");
    String userString = Scan.nextLine();
    
    userString = userString.toLowerCase();
    
    System.out.print("Enter a letter: ");
    char userChar = Scan.nextLine().charAt(0);
    
    
    userString = userString.substring(0, userString.length()-1);
    for (int i = 0; i < userString.length(); i++)
    {
    if (userString.charAt(i) == userChar)
    {
    charCount++;
    
    numWords++;
    }
    }
    
    System.out.println( numWords + " words contain the letter "+ userChar +".");
    System.out.println( charCount + " " + userChar + " are found in the sentence.");
    
    }
    }
    My apologies.

  6. #6
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Letter Count Program

    The point of the &#91;CODE]...&#91;/CODE] is to keep the formatting so the code is readable. There's no point using them on unformatted code.

    If you want to count the number of words that contain a character, you need to check the text word by word. You can use String.split("\\s") to split up the input text into an array of words.

    One of the principal objects of theoretical research in any department of knowledge is to find the point of view from which the subject appears in its greatest simplicity...
    J. W. Gibbs
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured