How would I write this program:

Wrte a program that will read a line of text that ends witha  period, which serves as a sentinal value. Display all the letters that occur in the text, one per line and in alphabetical order, along with the number of times each letter occurs in the text. Use an array of base type int of length 26, so that the element at index 0 contains the number of a's, the element and index 1 contains the number of b's and so forth. Allow both uppercase and lowercase letters as input, but treat uppercase and lowercase versions the same letter as being equal. Allow the user to repeat this task until the user says she or he is through. 

Edited 2 Years Ago by romdan94

correctly.
as variables you'll use, create two arrays: one of 26 chars {'a','b', ..., 'z'}
and one of 26 ints (by default 0 values)

you'll need an instance of Scanner. read using next, use a call to split to get the information up until the first '.'
do remember: only [a-z|A-Z] input is expected.
next, you change your String to lowercases and you sort the String you've read. this can be easily done by turning it into an array of chars, and sorting the elements.
iterate over the chars. for each char you encounter, add 1 to the appropriate element of the array of ints (which the appropriate one is, is explained in the assignment)
when you've done this, iterate over the array, again,
check if the number of occurences (in the int array is not 0).
if it is not, print the char, print the value stored in the int array, and set that value to 0, so you'll only print each char one time.

put all of the above in a while loop, and ask the user at the end of the loop whether or not to repeat the above.

hmm, I'd write it to the same standards as the assignment, which is written horrendously, and then confront the teacher with that when and if he grades me poorly (keeping a proper version at hand of course) ;)

There's a much simpler solution that stultusk's - no need for two arrays or sorting
Remember that chars in Java are integer values, and the alphbetic letters are represented by consecutive values, so you just need one array to hold the counts.
For each letter in the input:
.. convert lower case to upper case
.. subtract 'A' from it ... now 'a' or 'A' will both be 0, 'b' or 'B' will be 1 etc
.. use thatvalue as the index to increment the right element of the array of counts
when you have processed all the characters, print the characters 'A' to 'Z' and their correspoonding counts.

I programmed this myself as a challenge, could someone check it over to see if all the requirements were met?
Should I post it in a new Thread and link it here?

romdan doesn't seem to have contributed to his homework discussion at all, so it would be a pity to post a complete solution for him to copy and get the credit for.
Equally it would be a pity to deprive you of some constructive feedback. Maybe PM it to a few suitable candidates and ask for feedback?

Edited 2 Years Ago by JamesCherrill

This article has been dead for over six months. Start a new discussion instead.