Forums Register Login

Need help with computing the total number

+Pie Number of slices to send: Send
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

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
I think you should move 99% of that code out of the main method.
+Pie Number of slices to send: Send
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.
(1 cow)
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Scott, that is a great example. Have a cow!
+Pie Number of slices to send: Send
 

Paweł Baczyński wrote:Have a cow!



Thank you, Pawel!
It was the best of times. It was the worst of times. It was a tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 761 times.
Similar Threads
String Input and Bubble Sort
Radix Sort
Radix Sort
for loop keep stoping
hasNext and Files
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 04:42:09.