• 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

comparing digits in a number

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everybody:
I'm trying to compare the digits of a predetermined number (so that no two digits are the same). Is there a way to to this from an int variable? Or should I create an array? And if I do create an array, how do I get the integer number into an array (i.e.: "parse" integer into an array of digits)?

Thanks!
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This wouldn't happen to be a class assignment now would it?
I'll give you a hint:
<pre>
boolean[] digitsFound = new boolean[10]; // elements initialized to false by default
while (more digits) {
int nextDigit = ... ;
if (digitsFound[nextDigit])
// duplicate digit found!
// can exit loop now if you like
else
digitsFound[nextDigit] = true;
}
</pre>
You will want to use % and / (integer division) too...
Good luck and welcome to JavaRanch!

------------------
Junilu Lacar
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by JUNILU LACAR (edited October 19, 2001).]
[This message has been edited by JUNILU LACAR (edited October 19, 2001).]
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
You could just convert the int to a String, then loop through each character in that String and look for duplicates that way. The <code>charAt</code> method of the String class is one for you to look at.
Hope this helps, but not too much, figuring out the rest will be good for your soul, lol.
Michael

------------------
"One good thing about music - when it hits, you feel no pain"
Bob Marley
 
reply
    Bookmark Topic Watch Topic
  • New Topic