Write a program that determines how many of each letter a name has

Here is the instructions/examples for the program that I have to do:

Hello! Please enter your first name. Howdy

Thank you. Now enter your last name. Doody

Your first name is Howdy, and your last name is Doody. WHAT A NICE NAME YOU HAVE!!

Your first name contains 5 letters, and your last name contains 5 letters.

Your initials are: H D

Your name contains:

3 d's

1 h

3 o's

1 w

2 y's

Well, Howdy Doody, this has been fun. Good bye!

I can get all the way until the amount of letters in the name. My teacher said something about ASCII Character Set and that I only needed like 1 for loop. I do not know most of the array and other type stuff, and I want some help on how to make it into one loop. Here is my code so far:

    import java.util.Scanner;

public class CoolSet2Problem4
{
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Hello! Please enter your first name in all lower case:");
        String firstname = keyboard.next();
        System.out.println(); //this is just so it looks nice with another space
        System.out.println("Thank you. Now enter your last name in all lower case:");
        String lastname = keyboard.next();
        System.out.println(); //this is just so it looks nice with another space
        System.out.println("You first name is " + firstname + ", and your" + "\n" + "last name is " + lastname + ".");
        System.out.println("WHAT A NICE NAME YOU HAVE!!");
        System.out.println(); //this is just so it looks nice with another space
        int fnl = firstname.length();
        int lnl = lastname.length();
        System.out.println("Your first name countains " + fnl + " letters,");
        System.out.println("and your last name countains " + lnl + " letters.");
        System.out.println(); //this is just so it looks nice with another space
        char fni = firstname.charAt(0);
        char lni = lastname.charAt(0);
        System.out.println("Your initials are: " + fni + " " + lni);
        System.out.println(); //this is just so it looks nice with another space

    int acount = 0; int bcount = 0; int ccount = 0; int dcount = 0; int ecount = 0; int fcount = 0; int gcount = 0; 
    int hcount = 0; int icount = 0; int jcount = 0; int kcount = 0; int lcount = 0; int mcount = 0; int ncount = 0;
    int ocount = 0; int pcount = 0; int qcount = 0; int rcount = 0; int scount = 0; int tcount = 0; int ucount = 0;
    int vcount = 0; int wcount = 0; int xcount = 0; int ycount = 0; int zcount = 0;

    for (int k = 0; ( (k < fnl) && (k < lnl) ); k++)
    {
        if (firstname.charAt(k) == ('a' | 'A')) acount++;
        if (lastname.charAt(k) == ('a' | 'A')) acount++;
        if (firstname.charAt(k) == ('b' | 'B')) bcount++;
        if (lastname.charAt(k) == ('b' | 'B')) ccount++;
        if (firstname.charAt(k) == ('c' | 'C')) ccount++;
        if (lastname.charAt(k) == ('c' | 'C')) dcount++;
        if (firstname.charAt(k) == ('d' | 'D')) dcount++;
        if (lastname.charAt(k) == ('d' | 'D')) dcount++;
        if (firstname.charAt(k) == ('e' | 'E')) ecount++;
        if (lastname.charAt(k) == ('e' | 'E')) ecount++;
        if (firstname.charAt(k) == ('f' | 'F')) fcount++;
        if (lastname.charAt(k) == ('f' | 'F')) fcount++;
        if (firstname.charAt(k) == ('g' | 'G')) gcount++;
        if (lastname.charAt(k) == ('g' | 'G')) gcount++;
        if (firstname.charAt(k) == ('h' | 'H')) hcount++;
        if (lastname.charAt(k) == ('h' | 'H')) hcount++;
        if (firstname.charAt(k) == ('i' | 'I')) icount++;
        if (lastname.charAt(k) == ('i' | 'I')) icount++;
        if (firstname.charAt(k) == ('j' | 'J')) jcount++;
        if (lastname.charAt(k) == ('j' | 'J')) jcount++;
        if (firstname.charAt(k) == ('k' | 'K')) kcount++;
        if (lastname.charAt(k) == ('k' | 'K')) kcount++;
        if (firstname.charAt(k) == ('l' | 'L')) lcount++;
        if (lastname.charAt(k) == ('l' | 'L')) lcount++;
        if (firstname.charAt(k) == ('m' | 'M')) mcount++;
        if (lastname.charAt(k) == ('m' | 'M')) mcount++;
        if (firstname.charAt(k) == ('n' | 'N')) ncount++;
        if (lastname.charAt(k) == ('n' | 'N')) ncount++;
        if (firstname.charAt(k) == ('o' | 'O')) ocount++;
        if (lastname.charAt(k) == ('o' | 'O')) ocount++;
        if (firstname.charAt(k) == ('p' | 'P')) pcount++;
        if (lastname.charAt(k) == ('p' | 'P')) pcount++;
        if (firstname.charAt(k) == ('q' | 'Q')) qcount++;
        if (lastname.charAt(k) == ('q' | 'Q')) qcount++;
        if (firstname.charAt(k) == ('r' | 'R')) rcount++;
        if (lastname.charAt(k) == ('r' | 'R')) rcount++;
        if (firstname.charAt(k) == ('s' | 'S')) scount++;
        if (lastname.charAt(k) == ('s' | 'S')) scount++;
        if (firstname.charAt(k) == ('t' | 'T')) tcount++;
        if (lastname.charAt(k) == ('t' | 'T')) tcount++;
        if (firstname.charAt(k) == ('u' | 'U')) ucount++;
        if (lastname.charAt(k) == ('u' | 'U')) ucount++;
        if (firstname.charAt(k) == ('v' | 'V')) vcount++;
        if (lastname.charAt(k) == ('v' | 'V')) vcount++;
        if (firstname.charAt(k) == ('w' | 'W')) wcount++;
        if (lastname.charAt(k) == ('w' | 'W')) wcount++;
        if (firstname.charAt(k) == ('x' | 'X')) xcount++;
        if (lastname.charAt(k) == ('x' | 'X')) xcount++;
        if (firstname.charAt(k) == ('y' | 'Y')) ycount++;
        if (lastname.charAt(k) == ('y' | 'Y')) ycount++;
        if (firstname.charAt(k) == ('z' | 'Z')) zcount++;
        if (lastname.charAt(k) == ('z' | 'Z')) zcount++;
    }
    System.out.println(); //this is just so it looks nice with another space
    System.out.println(firstname + " " + lastname + " contains " + acount + " a\'s");
    System.out.println(firstname + " " + lastname + " contains " + bcount + " b\'s");
    System.out.println(firstname + " " + lastname + " contains " + ccount + " c\'s");
    System.out.println(firstname + " " + lastname + " contains " + dcount + " d\'s");
    System.out.println(firstname + " " + lastname + " contains " + ecount + " e\'s");
    System.out.println(firstname + " " + lastname + " contains " + fcount + " f\'s");
    System.out.println(firstname + " " + lastname + " contains " + gcount + " g\'s");
    System.out.println(firstname + " " + lastname + " contains " + hcount + " h\'s");
    System.out.println(firstname + " " + lastname + " contains " + icount + " i\'s");
    System.out.println(firstname + " " + lastname + " contains " + jcount + " j\'s");
    System.out.println(firstname + " " + lastname + " contains " + kcount + " k\'s");
    System.out.println(firstname + " " + lastname + " contains " + lcount + " l\'s");
    System.out.println(firstname + " " + lastname + " contains " + mcount + " m\'s");
    System.out.println(firstname + " " + lastname + " contains " + ncount + " n\'s");
    System.out.println(firstname + " " + lastname + " contains " + ocount + " o\'s");
    System.out.println(firstname + " " + lastname + " contains " + pcount + " p\'s");
    System.out.println(firstname + " " + lastname + " contains " + qcount + " q\'s");
    System.out.println(firstname + " " + lastname + " contains " + rcount + " r\'s");
    System.out.println(firstname + " " + lastname + " contains " + scount + " s\'s");
    System.out.println(firstname + " " + lastname + " contains " + tcount + " t\'s");
    System.out.println(firstname + " " + lastname + " contains " + ucount + " u\'s");
    System.out.println(firstname + " " + lastname + " contains " + vcount + " v\'s");
    System.out.println(firstname + " " + lastname + " contains " + wcount + " y\'s");
    System.out.println(firstname + " " + lastname + " contains " + xcount + " x\'s");
    System.out.println(firstname + " " + lastname + " contains " + ycount + " y\'s");
    System.out.println(firstname + " " + lastname + " contains " + zcount + " z\'s");
}

}

Any help will be appreciated.

I know this is long but it is the only way I know how.

Upvotes: 0

Views: 322

Answers (2)

umbreownage
umbreownage

Reputation: 16

Something like this should work instead. It can still be simpler and written better, though.

    //create a new array with the number of possible ascii characters
    int[] chars = new int[256];

    //loop through the length of first name
    for(int i = 0; i<fnl; i++) 
    {
        //Make lower case and save the char at index i and c
        char c = firstname.toLowerCase().charAt(i);
        //cast the saved char as a number and increment that position in the array
        chars[(int) c]++;
    }

    //repeat for last name
    for(int i = 0; i<lnl; i++) 
    {
        char d = lastname.toLowerCase().charAt(i);
        chars[(int) d]++;
    }

    //only want to print alphabetical letters a-z
    for(char e = 'a'; e <= 'z'; e++) 
    {
        //if the letter occurs 0 times, we start the loop at the next one
        if (chars[(int) e]==0)
            continue;
        else
            System.out.println("There are "+chars[(int) e]+" "+e+"'s in the String.");
    }

Upvotes: 0

Software Engineer
Software Engineer

Reputation: 16130

The simplest solution would be to use a 26 element array to represent the counts:

int[] letterCounts = new int(26);

You then loop through the letters in the names, something like

for (char ch: (firstname+lastname).toLowerCase().toCharArray()) {
}

In the body of the loop, convert the char, ch to an int, and subttract 97, which is the ascii code for a:

    int c = ((int)ch)-97;

This uses the fact that a char really is just an int under the covers -- the (int) bit 'casts' the char ch to be an int. Then use this to address the array:

letterCounts[c] = letterCounts + 1;

After the loop, you should loop again, this time to print out the array contents and the associated letters, like this:

for(int i = 0; i < 26; i++) {
  System.out.print(((char)i+97));
  System.out.printLn(" " + letterCounts[i]);
}

Here we're using a cast again, but this time we're casting an int i + 97 back to a char so that it can be printed. We've added 97 because when i is 0, adding 97 to it makes it 97, and when this is a char it's the letter a.

Upvotes: 1

Related Questions