1

I Think math is a little bit tricky in Java. Lets say I have an integer value of 203 and I want to extract 0 and 3. For me, the easiest way would be to have something like Intat(1) and Intat(2) or to be able to extract Left(203,1) to retrieve the "2" and then subract with: 203 - (2 * 100).

Anyway I think I got a solution, see this codeblock below:

    String Th = Integer.toString(203);
    int Th2 = Integer.valueOf(Th);
    String Th3 = Th.charAt(1) + "" +  Th.charAt(2);
    int Thrown_Card_Two_Post = Integer.valueOf(Th3);
    System.out.println(Thrown_Card_Two_Post);

Is there any more efficient way to accomplish this?

hokanovic
  • 59
  • 1
  • 11

2 Answers2

1

I think the best way is to do this from smaller to large.

  1. So first, take the modulus 10 to get the last digit. If you don't know, modulus 10is the remainder after division by 10 and can be expressed as 203 % 10.

  2. After you have the last digit, subtract it from the total and take modulus 100 to get the second digit.

  3. Repeat to get all digits.

If you don't need all digits, but only a specific one, you can first divide to skip a couple steps.

Thijs Steel
  • 1,190
  • 7
  • 16
0

you can use this trick : int number = 235 ;

  int ones = number % 10;
  int tens = (int)(number % 100) / 10;
Nadjib Bendaoud
  • 546
  • 1
  • 8
  • 19