• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Count number of times each letter occurs

 
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I'm trying to count how many times each letter occurs, but am only able to count the number of unique letters.
Sample input would is: ABBCCC
Sample output is: A = 1, B = 2, C = 3
This is what I have so far:

 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code looks like the sort of code you get when you just start writing code without a real plan - it's far more convoluted and complicated than it needs to be. May I suggest you get a pen and paper and write out in plain text the steps you would take to solve the problem yourself and then look to convert that to code.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to add more detail to my suggestion, Here's 2 basic ways of solving this written in plain text.

1. Look at each letter in turn and if it's not in my results list add it to the list and then iterate across the rest of the string to count how many times that letter occurs and write down the total number of occurrences.
2. Look at each letter in turn and if it's not in my results list add it to the list with the number 1 next to it. If it is in the list add 1 to the number next to it.
 
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Worth reading --> MainIsAPain
 
Marshal
Posts: 80867
505
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another thing worth reading is the Java™ Tutorials. I won't tell you which section about Maps will be particularly helpful.
 
reply
    Bookmark Topic Watch Topic
  • New Topic