• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Counting Matching Digits

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 789
Python C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic