[Solved] How to get the 3rd digit of a four-digit Integer?


In anticipation that it will be needed, here is how you get all the digits of a number.

      long value = Long.MAX_VALUE;

      System.out.println(value);

      while (value != 0) {
         // get the remainder when divided by 10
         int digit = (int) (value % 10);
         System.out.print(digit + " ");
         // now divide value by 10 to position for next digit.
         value /= 10;
      }
      System.out.println();

Note that they are printed in reverse order.

You can also do them individually

      first digit  =  x % 10
      second digit = (x/10) % 10
      third digit  = (x/100) % 10
        and so on

solved How to get the 3rd digit of a four-digit Integer?