wood burning stoves*
The moose likes Beginning Java and the fly likes Counting uppercase? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Counting uppercase?" Watch "Counting uppercase?" New topic
Author

Counting uppercase?

Stacey Johnson
Ranch Hand

Joined: Jan 11, 2004
Posts: 55
Hi,
I'm now working on a program that counts all the uppercase letters in a string entered in the command line arguement. When I try to compile I get an error with the first line below. I'm not too sure what the problem is. Can anyone help?


// Invoke the countLetters method to count each letter
int[] count = countLetters(word.isUpperCase());

// Display results
for (int i = 0; i < count.length; i++) {
if (count[i] != 0)
System.out.print((char)('a' + i) + " appears " +
count[i] + ((count[i] == 1) ? " time\n" : " times\n"));
}
// Count each letter in the string
public static int[] countLetters(String word) {
int[] count = new int[26];

for (int i = 0; i < word.length(); i++) {
if (Character.isLetter(word.charAt(i)))
count[(int)(word.charAt(i) - 'a')]++;
}

return count;

Thanks
Stacey
};
}
Maarten Vergouwen
Ranch Hand

Joined: Jan 12, 2004
Posts: 60
I dont see a declaration for 'word' outside of the countLetters method.
Unless that is done in a part you didnt post, it would result in an error because 'word' is used outside the scope of the countLetters method.
rgds,
Maarten
Nischal Topno
Ranch Hand

Joined: Nov 24, 2001
Posts: 45
Can you tell what is the compile time error you are getting? That will help us find out the issue fast
David Peterson
author
Ranch Hand

Joined: Oct 14, 2001
Posts: 154
The call to "isUpperCase()" should be "toUpperCase()".
Hope this helps.
David Peterson
Sadanand Murthy
Ranch Hand

Joined: Nov 26, 2003
Posts: 382
Stacey, it is always a good practice to specify what error you are getting. This will help the others to point you in the right direction. This is especially critical when only part of the code is posted. Some vars that we may think is not declared may have indeed been declared in the unposted code.


Ever Existing, Ever Conscious, Ever-new Bliss
 
wood burning stoves
 
subject: Counting uppercase?
 
Similar Threads
Return number of characters in a Word
Place A Word In A 2d Array
Counting uppercase? Part 3
Counting uppercase? Part 2!
How can I do this?