• Post Reply Bookmark Topic Watch Topic
  • New Topic

Counting Matching Digits  RSS feed

 
Erik Davies
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,
im taking a class and I need some help, as simple as this seems im am completely stumped.

supplied with a number i need to make a method that will return the number of times the given digit appears in the number

I could look this up other people answer but i would like work for the solution.

any advice or suggestions are greatly appreciated
Thank you in advance.

 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Erik Davies wrote: im am completely stumped.

Dear completely stumped,
If it was me I would convert both numbers to strings and do a substring search. The hint at the end of the code seems to imply you're supposed to do it mathematically though.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an exercise in using the / and % operators together to get the individual digits from an integer. At least I think it is.

[edit]Wrote & by mistake: changed to %[/edit]
 
Mahesh Kumaraguru
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


A very similar algorithm would work for finding the number of occurrences of a char within a String.

Note: - I posted a complete solution which did not show up in the replies, the message I got from JavaRanch staff was "attention required: your post requires editing before being shown in the forums / Please don't provide complete solutions.". So I am editing this post and converted part of it to pseudo code, maybe this policy is in place to "force" members to "learn Java" :-) Hoping the above "pseudo code + code" meets the JavaRanch posting standard.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would you isolate the right most digit using / or % ?
How would you shift the number to the right by one digit using / or % ?
How would you know when you're done ?
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahesh Kumaraguru wrote: . . .
Note: - I posted a complete solution which did not show up in the replies, . . . Hoping the above "pseudo code + code" meets the JavaRanch posting standard.
It is probably long enough since OP posted that you could no longer do any harm by posting solutions. I have restored the updated version of the post.
The solution you posted does not however fulfil the requirements for the original question. That was to operate on numbers using the == operator. It is completely unnecessary to use Strings and char[] arrays, unless possibly to get rid of the problem of having negative numbers.
 
Mahesh Kumaraguru
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
That was to operate on numbers using the == operator. It is completely unnecessary to use Strings and char[] arrays, unless possibly to get rid of the problem of having negative numbers.


I thought of a generic solution that would work with String, float, double, int and long, maybe using overloaded methods.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe, but how did you work it for ints?
 
Mahesh Kumaraguru
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Maybe, but how did you work it for ints?


Using

char[] charsInNumberToSearch = (Integer.toString(numberToSearch)).toCharArray();

Then did a char by char comparision and incremented a counter.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What a dreadful solution. I think it is long enough since the original post that we can post a real solution. Partially copied from the original post:-No need to use Strings or casts or char arrays.
Of course i would deduct 2 marks out of 10 because the name of the class is the same as a class in java.lang.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That solution will fall down completely if presented with a negative number field. I shall let people suggest how to correct that problem and find corner cases where correction is impossible.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
* must be at least 0 and at most 9.
Being too scrupulous

I'd change that line
to
otherwise I'd expect to see
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I preferred to use 10 because it is the first number which goes into two digits. You could do the same with
if (digit < 0 || digit >= 0x10)
… and you have the same format for hex numbers. But it is only a minor style point.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!