File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reg : program

 
ramkumar chennai
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai All,

Can write simple logic for the following program

Get the string from the user input (command line), and count the occurrence of each character and display in ascending order.

ex : Hello

output :

e: 1
h: 1
l: 2
0: 1


Thanks and Regards,
Ram kumar
 
Campbell Ritchie
Sheriff
Pie
Posts: 47292
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

That is the sort of thing we discuss on the beginners' forum. We don't simply hand out answers; you learn a lot more if you show us what you have managed already and we tell you how to improve it.

So please quote what you have already, using the code button, and the ranchers will be only too pleased to help. And please always make sure the subject of your topic says what the topic is about.
 
Vijitha Kumara
Bartender
Posts: 3908
9
Chrome Fedora Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried any code? If not as a starter you can think about using a Map (letter as the key and the count as the value). Try it and if you have any problem post it with your code. People will help you.
 
ramkumar chennai
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai All,

This is my code .


Please give me suggestion for fine tuning the code.

Thanks,
 
Rob Spoor
Sheriff
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are now iterating over the string, and for each character iterate over the string again. You can skip the second iteration (the inner loop) by using your Map to store the intermediate results:

Now you can improve the readability a bit if you are using Java 5.0 or higher, using both generics and autoboxing:

Remember that you cannot unbox a null Integer, that's why I have that check there.

Also note that I used Character as keys instead of Strings. That won't improve the code much, but that's the thing you actually want to keep the count for - characters, not strings.
[ September 09, 2008: Message edited by: Rob Prime ]
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:


as long as 'count' is less then 128
 
ramkumar chennai
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for your contribution
 
Rob Spoor
Sheriff
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rene Larsen:


as long as 'count' is less then 128

No, count will be boxed regardless. It will use a cached version if between -128 and 127 (inc) yes, but otherwise, it will just create a new Integer object when boxing.

Autoboxing uses Integer.valueOf(int value), which is something like this:
 
ramkumar chennai
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai All,

I am trying to be smart programmer, now i am lacking of logical skills.
Please post logical, technical (cover java concepts) related program type questions for my practice.
It will useful to be smart developer.

Advance thanks to all,
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic