Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Couting letters in Java [CODE]

    Hi all!
    I have a question on a java assignment that I have been working on for one of my classes and can figure it out. In this program we have to prompt for and read in any line of text, that can contain any number or words or characters including spaces and punctuation.
    It then prints out the number of times each letter of the alphabet occurs in that line. I have most of the program written but it is giving me a few compile errors; hence why I am here. The code is as follows:


    public static int indexOfLetter(char theLetter)
    {
    int index = Character.toLowerCase (theLetter)-'a';
    return index
    }

    import java.util.Scanner;
    public class CountLetter
    {
    public static void man(String[] args)
    {
    Scanner scan = new Scanner(System.in);
    int[] letterCount = new int[26];

    System.out.println("Please type in a line of text." + " You may use punctuation & spaces,");
    String line = scan.nextLine();
    int count = 0;
    int i = 0;
    while (i < letterCount.length)
    {
    letterCount[i] = 0;
    i++;
    }
    i=0;
    while (i < line.length())
    {
    if (countLetters.isLetter(line.charAT(i)))
    count++;
    i++;


    System.out.println("in the total number " + " of characters = "+count);
    i = 0;
    while (i < letterCount.length)

    if (letterCount[i]!=0)
    system.out.println("Number of " + (char)(i + 'a') + "\'s = " + letterCount[i]);
    i++;
    }

    }

    }



    These are the two errors it is returning in jgrasp:

    CountLetter.java:4: class, interface, or enum expected
    public static int indexOfLetter(char theLetter)
    ^
    CountLetter.java:7: class, interface, or enum expected
    return index
    ^

    Thanks in advance!!!

  • #2
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Edit: Thread moved to correct forum.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    This simply cannot be here:
    Code:
    public static int indexOfLetter(char theLetter)
    {
    int index = Character.toLowerCase (theLetter)-'a';
    return index
    }
    That must be moved into your class.

    Beyond this, you'll have a couple of other errors as well. One will be a compilation error, and the other will be a runtime error which is simply the placement of a closing brace. Since this is a homework assignment you should attempt to resolve these first, and come back if you have problems.
    Another thing to note is that in Java, an int[] will always default to 0. So the loop to populate with 0 is actually optional. This differs from an Integer[] which defaults to null (like Object[]).

    Also, in the future please wrap all code in [code][/code] or [php][/php] tags to preserve the formatting.


  •  

    Posting Permissions

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