Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Counting uppercase?

 
Stacey Johnson
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The call to "isUpperCase()" should be "toUpperCase()".
Hope this helps.
David Peterson
 
Sadanand Murthy
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic