• Post Reply Bookmark Topic Watch Topic
  • New Topic
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

Counting uppercase?

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
};
}
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The call to "isUpperCase()" should be "toUpperCase()".
Hope this helps.
David Peterson
 
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Everybody's invited. Even this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic