title: Need help creating method to return position of a number in an integer
body: <p>I'm working on a game called "Cows and Bulls". Basically, this is a number guessing game where player one picks a random, distinct 4-digit number, and player two tries to guess the number.</p>
  
  <p>For example,</p>
  
  <p>-Player one picks the random number: 1465
  -Player two guesses the number: 6432. Since the number player two guesses shares a common number (6), this number has one "Cow". A "Cow" is when the number player two guesses shares a common number with the random number player one picks, but it's not in the same place (the 6 is the first digit in player 2's number, whereas it's the third in player 1's number.)</p>
  
  <p>-If Player two guesses the number: 1466, there is 1 "Cow" and 3 "bulls". A "Bull" is when the number Player two guesses shares a common number(s) with Player 1's, AND it is in the same place.</p>
  
  <p>My problem is, I need to make a method that returns the position of a number in an integer.</p>
  
  <p>For example, if I have an integer "1456", I need a method that tells me what position (from the right) any given number is. So if I want to know where the number "5" is, it would tell me "2" because it is second from the right. If I wanted to know where "1" is, it would tell me "4" because it is fourth from the right.</p>
  
  <p>I <strong>cannot</strong> use any of the built in java string methods. </p>
  
  <p>Below is what I have wrote so far, it's not organized at all as I am still working on it. </p>
  
  <p>This method has to call to the methods "numDigits" and "getDigit", thats why I left them in the code. "numDigits" returns the number of digits in a number. "getDigit" returns what number is in a particular position. So, if I have a number "12345", getDigit will return "2" if I input "4" because "2" is fourth from the right in the number.</p>
  
  <p>You can see I started "indexOf" at the bottom of my code.</p>
  
  <p>Sorry if I haven't been clear enough, I'm new to Java so I don.t know the best way to explain everything.</p>
  
  <pre><code>package pa3;
  import java.util.*;
  import java.util.Scanner;
  
  public class CowsAndBulls {
  
      /**
       * @param args
       */
      public static void main(String[] args) {
          //System.out.println("Welcome to Cows and Bulls Game. I picked a random 4-digit number with distinct integers, try finding it. Type your guess, must be a 4-digit number with distinct digits.");
          //System.out.println(numDigits(12346));
          System.out.println(getDigit(12345, 5));
          System.out.println(hasDistinctDigits(1234));
      }
      public static int numDigits(int number)
      {
          {
  
              int counter = 0;
  
              while(number &gt; 0)
              {
              number = number/10;
              counter++;
              }
  
              return counter;
              }
      }
  
  
          public static int getDigit(int number, int i)
          {
              {
                  int digit = 0;
  
                  if (i &lt;= numDigits(number))
                  {
                      while (i &gt; 0)
                      {
                           digit = number % 10;
                          number = number / 10;
                              i--;
                      }
                  }
                  return digit; }
          }
  
  
  
  
  
  
          public static boolean hasDistinctDigits(int number)
          {
              if(getDigit(number, 1) != getDigit(number, 2) &amp;&amp;
                getDigit(number, 1) != getDigit(number, 3) &amp;&amp;
                getDigit(number, 1) != getDigit(number, 4) &amp;&amp;
                getDigit(number, 2) != getDigit(number, 3) &amp;&amp;
                getDigit(number, 2) != getDigit(number, 4) &amp;&amp;
                getDigit(number, 3) != getDigit(number, 4))
                {
                    return true;
                }
                else return false;
  
              }
  
          public static int generateSecretNumber()
          {
              Random r = new Random();
              int randNum = r.nextInt(9999);
  
              while (hasDistinctDigits(randNum) = true)
                  {       
                          randNum = r.nextInt();  
                          return randNum;
  
                  }
  
          }
          public static void indexOf(int number, int digit)
          {
  
          }
  
  
  
  
  }
  </code></pre>