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.


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

Results 1 to 3 of 3

Thread: need help printing single error

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Thanked 0 Times in 0 Posts

    Default need help printing single error

    Here's my code. I need to figure out how to print the specific character in the string (say I input word5 - the the erroneous character would be the 5) that keeps the code from working.

    // ************************************************** **************
    // CountLetters.java
    // Reads a words from the standard input and prints the number of
    // occurrences of each letter in that word.
    // ************************************************** **************
    import java.util.Scanner;
    public class CountLetters
    public static void main(String[] args)
    int[] counts = new int[26];
    Scanner scan = new Scanner(System.in);
    // Read word from user
    System.out.print("Enter a single word (letters only, please): ");
    String word = scan.nextLine();
    // Convert input to all upper case
    word = word.toUpperCase();
    // Count frequency of each letter in string
    try {
    for (int index=0; index < word.length(); index++)
    // Print frequencies of letters
    for (int index=0; index < counts.length; index++)
    if (counts [index] != 0)
    System.out.println((char)( index +'A') + ": " + counts[index]);
    catch (ArrayIndexOutOfBoundsException e) {
    System.err.println( word +" is not just letters, dude.");
    Last edited by ash12; July 21st, 2012 at 04:40 PM.

  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    My Mood
    Thanked 263 Times in 232 Posts

    Default Re: need help printing single error

    you again, without code tags again.....

  3. #3
    Join Date
    Jun 2012
    Left Coast, USA
    My Mood
    Thanked 97 Times in 88 Posts

    Default Re: need help printing single error

    After converting to upper case, you use this statement to increment one of the 26 counters corresponding to characters 'A' - 'Z' right?

    If the current character is not in the range 'A' through 'Z', the index value will be out of bounds (less than zero or greater than 25). So, you have a way and a place to identify the Bad Guy right there so that you can not only report the specific problem, but you can prevent the "index out of bounds" exception from happening. (Test the character before trying to use it in the increment statement.)


    Last edited by Zaphod_b; July 22nd, 2012 at 02:21 PM.

  4. The Following User Says Thank You to Zaphod_b For This Useful Post:

    ash12 (August 5th, 2012)

Similar Threads

  1. Single Character Validation
    By dannybrgc in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 21st, 2012, 06:20 PM
  2. Replies: 1
    Last Post: September 28th, 2011, 07:29 AM
  3. Error In Printing. Perhaps a nesting problem
    By Pingu00 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 23rd, 2011, 07:46 AM
  4. Replies: 3
    Last Post: April 11th, 2011, 09:51 PM
  5. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM