Results 1 to 4 of 4
  1. #1
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Program not counting all of the letters its suppose to be

    I'm writing a program that will count the number of occurrences of a certain letter. This is my code and my current output. The answer of the output is '8' while I'm only getting '7'. Does anybody see the problem? I can't use Arrays for this problem.

    Java Code:
        public int letterCount(char c)
        {
        	int total = 0;
    		for (int i=0; i< m_str.length(); i++)
    		{
    			if (Character.isUpperCase(m_str.charAt(i)))
    				total++;
    		}
    		return total;
        }
    Output:

    Starting with: She Sells Sea Shells Down by the Sea Shore
    Number of s's = 7

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,947
    Rep Power
    4

    Default Re: Program not counting all of the letters its suppose to be

    You are counting upper case letters, not occurrences of a specific letter.

    There are a number of ways to approach the problem. But first you need to determine if this is a case-sensitive count or not.

    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Re: Program not counting all of the letters its suppose to be

    I don't want it to be case sensitive, but when I tried other methods it started returning the number of total letters in the sentence and not the number of s's

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,947
    Rep Power
    4

    Default Re: Program not counting all of the letters its suppose to be

    Okay. It will be case-insensitive. But notice that you are not comparing the supplied character to anything. Before coding you need to think of a strategy or general approach. Here is an example:


    1. Get the character to count.
    2. Compare this character to each character in the string.
    3. If a match, increment count.


    Now that is a reasonable start. However, in the general case, it will not work correctly. What happens if someone wants to count all the c's so they provide a C as an argument.

    To test this you might want to put in strings of the same character but mix up the case and see what happens. And as always, put in print statements to help debug.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. how to do these program letters with values
    By w0ooop in forum NetBeans
    Replies: 16
    Last Post: 02-20-2013, 03:26 PM
  2. Finding & counting certain letters in a sentence
    By LetsG0Blue in forum New To Java
    Replies: 1
    Last Post: 02-19-2013, 07:15 AM
  3. counting letters in a string
    By beandip408 in forum New To Java
    Replies: 12
    Last Post: 09-29-2010, 01:44 PM
  4. Replies: 2
    Last Post: 04-09-2009, 10:46 PM
  5. Need help with counting letters
    By mrdestroy in forum New To Java
    Replies: 15
    Last Post: 10-22-2008, 01:33 PM

Posting Permissions

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