• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Need help with computing the total number

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For example, a user input a number of country into a queue such as Iceland, Italy, Spain,Switzerland and Singapore . How do I compute the total number of country name that begins with the same letter?
The outcome should be
Total number of countries start with I : 2
Total number of countries start with S : 3

 
Marshal
Posts: 76392
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you look in the Java™ Tutorials, you will find an example of counting words. You should be able to amend that to map first letter ↦ count.
 
Campbell Ritchie
Marshal
Posts: 76392
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you should move 99% of that code out of the main method.
 
Marshal
Posts: 8370
594
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Probably would be better if you wouldn't cram all code into main method, but instead create appropriately named method for your each task, so you wouldn't need write such long comments.

Joan Tan wrote:How do I compute the total number of country name that begins with the same letter?

Have you tried with charAt or substring methods? Might one of these would work.
 
Greenhorn
Posts: 16
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Joan --

It is definitely wise to start with Campbell and Liutauras's advice to break this problem down into tasks, then code each task in its own method. If you were doing this by hand, how would you summarize the broad steps? For example, if I were to try to encode my process of responding to this post, I might have methods that look like...


Then my main() method looks like this...

I know I'm over-explaining this, but it's a good practice start with this type of logical breakdown.

By the way, I illustrated this using Java code, but it is far more important that you do this kind of breakdown on paper, before you start coding.

Then, once you've figured out the logic on paper, you can try encoding each method, one step at a time. What I'm responding to (and maybe Campbell and Liutauras feel similarly) is that there are a number of issues with your draft code, starting all the way back at line 10 (or even earlier if you're missing necessary import statements). Once you've broken this down, you can get help on each piece.

--Scott
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Scott, that is a great example. Have a cow!
 
Scott Bee
Greenhorn
Posts: 16
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:Have a cow!



Thank you, Pawel!
 
The moth suit and wings road is much more exciting than taxes. Or this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic