Friends, how to find out the first digit of an array element? I figured out the last one, but from the first difficulty.

for example, given the array a=[352, 234,745,853]

withdraw 3, 2, 7 ,8

  • 3
    Converting a number to a string and getting the first character of the string, for example. - Vladimir Martyanov
  • one
    Who voted for the closing, post with a comment that you do not like in the question. In my opinion, the question is good, not simple, except that the array is out of place. - VladD

3 answers 3

The version of Vladimir Martyanov from the comments (about converting to a string and selecting a character in the first position) is good.

Alternatively, you can do this:

  1. If the number is zero, then output 0, take the next number and check again. When the number will no longer be 0, go to step 2.
  2. Find from the decimal logarithm
  3. We discard the mantissa (we get a number equal to the digit capacity - 1)
  4. We build 10 to the degree obtained
  5. We discard the mantissa (because Math.pow returns a double )
  6. We divide the original number by that obtained in paragraph 5 (the first digit is obtained)

Such code:

 int a[] = {352, 2354,745,853}; for (int i : a) { if (i==0) { System.out.println(0); continue; } if ( i == Integer.MIN_VALUE ) { System.out.println(2); continue; } System.out.println(i / (int)Math.pow(10, ((int)(Math.log10(Math.abs(i)))))); } 

Output:

 3 2 7 8 

UPDATE

As suggested by @VladD, you need to separately consider the case of Integer.MIN_VALUE . The fact is that Math.abs with i < 0 returns -i , and for int it is already going beyond 2147483647. Therefore, the number returns to its negative position.

  • Math.log10(Math.abs(i)) will crash in one tricky case :) - VladD
  • @VladD, at zero? I reviewed it separately. - LEQADA
  • 2
  • @VladD, corrected, thanks. - LEQADA
  • You are welcome! (I just fell into the same trap.) - VladD

Another option:

  1. Check whether the number exceeds 10 (inclusive)
  2. If yes, divide by 10 without remainder and GOTO 1.
  3. If not, output as result.

Code:

 int a[] = {352, 2354, 745, 853}; for (int i : a) { // если число может быть отрицательным, добавляем следующую строку. i = Math.abs(i) while (i >= 10) { i = i / 10; } System.out.println(i); } 
 3 2 7 8 

I hope to output in one line and with random commas ( 3, 2, 7 ,8 ) is this not a mandatory requirement? =)

  • i = Math.abs(i) not quite true. Guess what value i ? - VladD
  • @VladD minint more precisely, but the essence is clear. ) I'll think about how to handle it beautifully. - Nick Volynkin
  • Exactly! [15 characters] - VladD

Implementation based on Integer.toString and company. In principle, the same logarithm, but according to the table.

 final static int[] placements = new int[] { 0, 9, 99, 999, 9_999, 99_999, 999_999, 9_999_999, 99_999_999, 999_999_999, Integer.MAX_VALUE }; static int firstDigit( int number ) { // т.к. Math.abs( Integer.MIN_VALUE ) == Integer.MIN_VALUE // выносим в отдельную проверку if ( number == Integer.MIN_VALUE ) return 2; number = Math.abs( number ); int position = 1; while ( number > placements[position] ) { position++; } return number / (placements[position-1]+1); } public static void main(String args[]) { int a[] = {352, 2354, 745, 853}; // Ня! Стримы! System.out.println( Arrays.stream( a ).map( n -> firstDigit( n ) ) .mapToObj( Integer::toString ).collect( Collectors.joining( ", " ) ) ); } 
  • Oh, you did not fall into the trap. +1 - VladD
  • one
    This is because I stumbled on the for( int n = 0; n <= Integer.MAX_VALUE; n++ ) process during the test for( int n = 0; n <= Integer.MAX_VALUE; n++ ) :) - zRrr