programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Methods and Arrays Help

Greenhorn
Posts: 2
• Number of slices to send:
Optional 'thank-you' note:
Hello everyone.

I am completely lost with this assignment my professor gave us a few weeks ago. The due date has passed and I ended up submitting a basically blank code. However, I would still like to know how to do this. I just don't even know where to start. I think the multiple methods we have to use are confusion me. Here is what my professor wanted us to do:

Objective: Write a program that reads in lines from the input. For every line, it counts each letter of the English alphabet. It also counts the number of vowels, consonants, and others. It then prints out this information.You need to create and use and array that keeps track of the count for each letter.

Example input:
hevdhfewfewfe
hhwwrrr73##\$6%&%7

Example output:
Line 01: d=1 e=4 f=3 h=2 v=1 w=2 YES vowels=4 consonants=9 others=0
Line 02: h=2 r=3 w=2 NO vowels=0 consonants=7 others=10

What to do:
1. You will need to declare a static int array variable that will keep track of the counts for each letter. This array should have length 26. Array Index 0 will be used to store count for the letter 'a'. Array Index 1 will be used to store count for the letter 'b'. Array Index 25 will be used to store count for the letter 'z'. Suppose the array is named letterCountArray. Then to access the index for character c, you will need to do c- 'a'. Thus, if c is 'm', your code will access index 'm' - 'a' - which is 12.

2. private static void addLetterCount(char c)
This method is to add to the count for the letter indicated by the variable c. Basically, index into the array declared above and add to the count for the letter indicated by variable c.

3. private static void zerroLetterCount()
Zero out all the elements of the array.

4. private static void printLetterCounts()
Loop over the array and print the counts for each letter. Thus, if there are only two counts that are not zero say 'a' has count 5 and 'z' has count 10, then this method will print "a=5 z=10 " (Note the space after 10).

5. private static int countVowels()
This method returns the sum of the count of all the vowels (i.e. for 'a', 'e', 'i'. 'o', 'u').

6. private static String anyVowels()
This method returns the String 'NO" if countVowels() returns 0. Otherwise it returns the String "YES"

7. private static int countConsonants()
This method returns the sum of counts of all the consonants. (Just add all elements of the array and subtract countVowels().

8. private static void counts(int lineNum, String s)
This method is the "workhorse" of the program. It is given a line number and the line as input. It first loops over each character of the line and adds to the appropriate array element if it is a letter. Otherwise, it adds to "Other". Then it prints the information for the line as described earlier. To print the information, make use of the methods described earlier. Use printf with formal "Line%02d:" to be able to print the line number with LEADING zeroes.

9. main method.
In the main method, keep count of the line number and make calls to "counts" method as needed.

I know I don't have much but some guidance or any sort of help would be greatly appreciated.

Saloon Keeper
Posts: 10653
85
• Number of slices to send:
Optional 'thank-you' note:
There are a number of goals, I suggest starting with code to read a file and just get that to work before selecting the next goal, get that to work, etc..

lowercase baba
Posts: 13089
67
• Number of slices to send:
Optional 'thank-you' note:
Did you write that code, or was it given to you?  I'd say the first thing you need to do is get it to compile - it throws a single error as is.  Once you fix it, you'll get a few more errors.  So i'd probably comment out everything from line 20 through line 35.

As Carey said, you want to try and do as little as possible at a time.  Do you know what your Scanner is for, and how to use it?

Marshal
Posts: 79061
375
• Number of slices to send:
Optional 'thank-you' note:
...and welcome to the Ranch

katherine nagel
Greenhorn
Posts: 2
• Number of slices to send:
Optional 'thank-you' note:

Carey Brown wrote:There are a number of goals, I suggest starting with code to read a file and just get that to work before selecting the next goal, get that to work, etc..

Yea, that makes sense. I think I am just getting really overwhelmed.

fred rosenberger wrote:Did you write that code, or was it given to you?  I'd say the first thing you need to do is get it to compile - it throws a single error as is.  Once you fix it, you'll get a few more errors.  So i'd probably comment out everything from line 20 through line 35.

As Carey said, you want to try and do as little as possible at a time.  Do you know what your Scanner is for, and how to use it?

I wrote the code as a guideline but I do see your point. It kind of makes things confusing having everything written out like that. Yes, I do know what the Scanner is for and how to use it. Basically from my understanding is that it reads the user input. I actually created a code similar to this one with less methods and less arrays. I think what is really tricking me up here is all of the different methods we have to use and the different arrays.