Make this java program as less complicated as possible. input a no. and print how many times each no. appears.?
-
like if i print 123; it print 1-1 2-1 3-1 3432 prints 2-1 3-2 4-1
-
Answer:
you can iterate over the string by turning it to char array then check if it is 0 - 9 and add or increment the ammount then print only the one those that are not 0 let say int[] amt = int[10] ; // an array of int for the appearances from 0 - 9 there are only no. right ;) char[] ca = input.toCharArray ; // iterate the characters of the input for ( int x = 0 ; ca.length > x ; x++) // note x++ is a shortcut for x = x + 1 { switch (ca[x]) // im using switch because its much shorter but if you want to if-then-else go ahead { case '0' : amt[0] = amt[0]++; break; case '1' : amt[1] = amt[1]++; break; case '2' : amt[2] = amt[2]++; break; } } String out = new String(); // the output string for ( int y = 0 ; y >= amt.length ; y++) { if ( amt[y] > 0 ) { out.append( y + "-" + amt[y] + ' \n ') ; // the format should be the same like your example // but im not sure if \n is the correct newline character } } System.out.print(out);
Aranya at Yahoo! Answers Visit the source
Related Q & A:
- How many times can paper be recycled?Best solution by Yahoo! Answers
- Is there any way of seeing how many times a Wikipedia page has been viewed?Best solution by Yahoo! Answers
- Is there a limit of how many times one can study abroad?Best solution by Yahoo! Answers
- How many times can you recycle paper?Best solution by indianapublicmedia.org
- How many times can you Tweet a day?Best solution by ChaCha
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.