wood burning stoves 2.0
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

Counting uppercase?

Stacey Johnson
Ranch Hand

Joined: Jan 11, 2004
Posts: 55
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;

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.
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
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
I agree. Here's the link: http://aspose.com/file-tools
subject: Counting uppercase?
It's not a secret anymore!