3

How do you check a single digit in an int? I know for Strings you can use charAt(), but if you try to use it for an int it will return an error message. Is there any way to check a single digit and set it to a variable in a multiple digit number?

babyguineapig11
  • 355
  • 3
  • 17
  • 2
    Convert int to String and use charAt, or use something like `(x/10...0)%10` (depending on digit you want to get). – Pshemo Dec 01 '15 at 22:25
  • 1
    @KickButtowski Regex works on `String` (text), not on `int`. – Pshemo Dec 01 '15 at 22:27
  • 3
    http://stackoverflow.com/questions/3389264/how-to-get-the-separate-digits-of-an-int-number – Atri Dec 01 '15 at 22:29

1 Answers1

4

You can convert int to String and can use charAt as follow

int a = 566;
String s = Integer.toString(a);
System.out.println(s.charAt(0)); 

Output: 5

ashiquzzaman33
  • 5,781
  • 5
  • 31
  • 42