Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with computing the total number  RSS feed

 
Joan Tan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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

 
Campbell Ritchie
Marshal
Posts: 55687
163
  • Mark post as helpful
  • send pies
  • 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: 55687
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you should move 99% of that code out of the main method.
 
Liutauras Vilda
Marshal
Posts: 4636
316
BSD
  • Mark post as helpful
  • send pies
  • 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.
 
Scott Bee
Greenhorn
Posts: 16
2
  • Mark post as helpful
  • send pies
  • 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
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:Have a cow!


Thank you, Pawel!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!