File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Reg : program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reg : program" Watch "Reg : program" New topic
Author

Reg : program

ramkumar chennai
Greenhorn

Joined: Sep 08, 2008
Posts: 16
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

Joined: Oct 13, 2005
Posts: 37970
    
  22
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

Joined: Mar 24, 2008
Posts: 3817

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.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
ramkumar chennai
Greenhorn

Joined: Sep 08, 2008
Posts: 16
Hai All,

This is my code .


Please give me suggestion for fine tuning the code.

Thanks,
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

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 ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

Originally posted by Rob Prime:


as long as 'count' is less then 128


Regards, Rene Larsen
Dropbox Invite
ramkumar chennai
Greenhorn

Joined: Sep 08, 2008
Posts: 16
Thanks all for your contribution
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

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

Joined: Sep 08, 2008
Posts: 16
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,
 
jQuery in Action, 2nd edition
 
subject: Reg : program
 
Similar Threads
Tomcat as a service
WA #1.....word association
direct connection (Networking)
Delete Expired Rows?
incremetor