Results 1 to 16 of 16
  1. #1
    mrdestroy is offline Member
    Join Date
    Oct 2008
    Posts
    22
    Rep Power
    0

    Question Need help with counting letters

    How can I count the number of times
    a character repeated in the String (ignore case).:confused::confused:

  2. #2
    ojn
    ojn is offline Senior Member
    Join Date
    Aug 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    Have you even bothered reading about the String class in the API? Read first, then code, then ask specific questions about your code.

  3. #3
    hkansal is offline Member
    Join Date
    Oct 2008
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by ojn View Post
    Have you even bothered reading about the String class in the API? Read first, then code, then ask specific questions about your code.
    Hey, pls don't be so harsh on the new guy. might be he missed out the research part.

    @mrdestroy: You actually do not need to know the String class to write a similar logic urself. basic procedural programming ll do. but as ojn said about the String class, yes.. there is a related method.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Do you have a single letter to look for or are you making a count for all the letters in the String?
    There are several ways to get to the individual characters/letters in a String. Look at the API doc for the string class. substring, indexOf and charAt for example.

  5. #5
    mrdestroy is offline Member
    Join Date
    Oct 2008
    Posts
    22
    Rep Power
    0

    Default

    I mean the number of 1 letter repeated in a String.
    Ex: javaforum
    There are 2 letter a repeated.
    I'm reading the API, but pls give some ideas if you have.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Do you want a count for all the letters or for just a single letter that you have chosen?

    Which method do you think you want to use? Can you code a couple of lines of code to show us how you plan to use it?

  7. #7
    mrdestroy is offline Member
    Join Date
    Oct 2008
    Posts
    22
    Rep Power
    0

    Default

    public class LetterCounting {

    /**This is the problem.
    * Write a program to ask the user to enter a String which consists of only alphabet
    letters a-z or A Z and a character. Then your program will count the number of times
    this character repeated in the String (ignore case). ‘A’ is considered different with ‘a’.
    Note: You no need to validate the user’s input for this exercise.
    */
    public static void main(String[] args) {
    String input, check;
    int l, times = 0;

    input = JOptionPane.showInputDialog("Please enter a string: ");
    l = input.length();

    check = JOptionPane.showInputDialog("Please enter a letter to check: ");

    for (int i = 0; i < l ; i++){
    JOptionPane.showMessageDialog(null, "Letter " + check + " repeats " + times + " in string " + input );
    //times here is the number of repetition of a letter. How can I count this?

    }
    }
    }
    //this is a few codes I just plan to do. But I'm on stuck. How can I do it in while loop?

  8. #8
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Think about it ...

    Part of the instructions seem contradicting:
    ... (ignore case). A is considered different with a.
    Either it's case sensitive or 'A' is considered that same as 'a'

    Anway.. This is what I would do:
    • Read/get entire text
    • In the loop compare each text character to the letter you are looking for (hint: do not use ==)
    • When a the same letter is found, increment a letter counter
    • When finished looping through the text, print the letter counter variable

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  9. #9
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    Of... use the split function to split on that letter and use the length of the array it returns -1. ;)
    I die a little on the inside...
    Every time I get shot.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Forget about all the I/O stuff. Write a simple program that has a string defined in it, like: String data = "Scan this for an a":
    and some code to scan that String for the character and count its occurances.

    Then merge it in with the I/O code you've shown.

    @Supamagier:
    Let the OP learn to program first, then show him the shortcuts.
    He must learn loops.

  11. #11
    mrdestroy is offline Member
    Join Date
    Oct 2008
    Posts
    22
    Rep Power
    0

    Default

    public static void main(String[] args) {
    String input;
    int l, times = 0;
    char c1, c2;

    input = JOptionPane.showInputDialog("Please enter a string: ");
    l = input.length();

    c1 = JOptionPane.showInputDialog("Please enter a letter to check: ").charAt(0);

    for (int i = 0; (i <= l - 1) ; i++){
    c2 = input.charAt(i);
    if (c1 == c2)
    times++;
    }
    JOptionPane.showMessageDialog(null, "Letter " + c1 + " repeats " + times + " time(s) in string " + input );
    }
    }

    Is there any ways using while or do while pls?

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  13. #13
    mrdestroy is offline Member
    Join Date
    Oct 2008
    Posts
    22
    Rep Power
    0

    Default

    Yes, the same code but using do-while or while.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    c1 = JOptionPane.showInputDialog("Please enter a letter to check: ").charAt(0);
    Do this in two or more steps to be able to test what the user entered before assuming it is valid and assigning it to c1.

    The for loop is more natural when doing a scan. It sets the index, tests for the end and increments it all in one spot. You can do the incrementing of the index manually in a while loop and use the same end of loop condition test as in the for loop.

  15. #15
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Default

    int count=0;
    int i;


    for (i = 0; i < input.length(); i++)
    {
    if (input.charAt(i)== c1)
    {
    count++;

    }
    }

    //For loop is the way to go for counting the letters. I think this is how I did it, correct me if I'm wrong :)

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Counting numbers up and down
    By radio in forum New To Java
    Replies: 4
    Last Post: 05-06-2011, 03:03 PM
  2. Counting the number of columns in a 2D array,
    By KalEl in forum New To Java
    Replies: 9
    Last Post: 10-21-2008, 05:27 AM
  3. Letter with Letters
    By elgatoboricua in forum New To Java
    Replies: 7
    Last Post: 09-16-2008, 02:59 PM
  4. Counting Pixels
    By shaungoater in forum Java 2D
    Replies: 5
    Last Post: 11-29-2007, 05:51 PM
  5. Counting Vowels and Constonants
    By MattN in forum New To Java
    Replies: 3
    Last Post: 11-20-2007, 05:45 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
  •