Results 1 to 3 of 3
  1. #1
    j@v@ is offline Member
    Join Date
    Oct 2010
    Location
    Baltimore, MD
    Posts
    5
    Rep Power
    0

    Default Counting char in String

    Hello all,

    Just seeking some advice in reference to a homework problem I have for my Java 101 class. Basically it accepts a String as a parameter, and returns the number of times character 'A' is displayed. I can not seem to get the method countA down..

    num = countA(length, String1, a);

    public static String countA(int length, String String1,char a)
    {
    int counter = 0;

    for (int i=0; i<length; i++)
    {
    if (String1 == 'a')
    {
    counter++;
    }
    return count;
    }

    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    String1 (which should be string1) is the whole String, right? So when would it ever equal a single char? Would it even equal a single char if it was a single char? Does "a" == 'a'? Try it out.

    You want to compare the char at each index of the String to 'a'. Check out the String API for useful functions.

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    Quote Originally Posted by j@v@ View Post
    Hello all,

    Just seeking some advice in reference to a homework problem I have for my Java 101 class. Basically it accepts a String as a parameter, and returns the number of times character 'A' is displayed. I can not seem to get the method countA down..

    num = countA(length, String1, a);

    public static String countA(int length, String String1,char a)
    {
    int counter = 0;

    for (int i=0; i<length; i++)
    {
    if (String1 == 'a')
    {
    counter++;
    }
    return count;
    }

    }
    :eek::eek:
    you will return a number, why do you write, that countA is returning a String in the method declaration? ;/

    if (String1 == 'a') - String1 is a string and you trying to compare it with a char? makes no sense or? (String1.charAt(i) == a). you should use a instead of 'a' or remove the parameter..

    move the return count(which you should rename to counter) one line down!

Similar Threads

  1. check a string char by char
    By Sotsiak in forum New To Java
    Replies: 2
    Last Post: 10-23-2010, 10:24 PM
  2. counting letters in a string
    By beandip408 in forum New To Java
    Replies: 12
    Last Post: 09-29-2010, 02:44 PM
  3. Char to Bit String
    By Krooger in forum New To Java
    Replies: 2
    Last Post: 01-29-2010, 03:26 AM
  4. char to string
    By kian_hong2000 in forum New To Java
    Replies: 2
    Last Post: 08-25-2008, 02:51 PM
  5. Help with, String, Char
    By lenny in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 03:58 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
  •