Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Count Number of Each Letter in Given Word?

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Count Number of Each Letter in Given Word?

    Hi, So I'm trying to write a program in which the user enters a word, then the program displays each letter of the alphabet and how many times each letter is used in that word.
    Ex. "Testing" A-0 B-0 .... S-1 T-2
    Like that

    However, I don't know whats wrong with the code and how to fix it?
    Any help is appreciated

    import java.lang.String;
     
    public class Words
    {
    public static void main(String[]args)
    {
    String words = new String();
    words = Input.getString("Please enter a statement");
     
    int[]total = totalLetters(words.toLowerCase());
     
    for (int i=0; i < total.length; i++)
    {
    if (total[i]!=0) 
    System.out.println("Letter " + (char)('a' + i) + " count = " + total[i]);
    }
    }
     
    public static int[]totalLetters(String words)
    {
    int[]total = new int[26];
     
    for (int i=0; i < words.length(); i++) 
    {
    if (Character.isLetter(words.charAt(i))) 
    {
    total[words.charAt(i) - 'a']++ ;
    }
    }
     
    return total;
     
    }
    }


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Count Number of Each Letter in Given Word?

    However, I don't know whats wrong with the code
    What makes you think there's something wrong with your code?

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Count Number of Each Letter in Given Word?

    I keep getting an error message about "input"

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Count Number of Each Letter in Given Word?

    Please post the full text of your error message here so we can see it.

Similar Threads

  1. Replies: 3
    Last Post: January 28th, 2011, 09:37 AM
  2. Where I'm I wrong? I need to do a count of the number of each element in an array
    By NavagatingJava in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 21st, 2011, 02:50 AM
  3. Java Dos Logic Test count all non increasing number
    By Jhovarie in forum Object Oriented Programming
    Replies: 3
    Last Post: January 13th, 2011, 03:28 PM
  4. Sentence and Letter Count Program
    By velop in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 10th, 2010, 12:10 AM
  5. letter to number
    By silverspoon34 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 27th, 2009, 07:01 AM