5

So the question is very simple. How to check in java if first digit of an int is 0;

Say I have:

int y = 0123;
int n = 123;

Now I need an if statement that would return true for y and false for n.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Freddy19
  • 167
  • 4
  • 10
  • 4
    That's not possible. The 0 is removed at compile time. During runtime, the values are the exact same, making your check impossible. If you were using ``String``s to store these values, it would be possible. – f1sh Apr 30 '18 at 11:17
  • Maybe you could also tell us why you think a leading non significant zero matters to you. Mathematically speaking, it should not matter. – Tim Biegeleisen Apr 30 '18 at 11:21
  • 4
    @f1sh Actually, prefixing an int with a "0" in this case turns it into an octal value, so they're not the same in all cases (including this one, the one you prefixed with a 0 would have a decimal value of 83, so you *could* tell to a certain degree.) But I'm not sure that's what you're after, and other than that, it's not possible to detect this at runtime. – Michael Berry Apr 30 '18 at 11:22
  • 1
    The reason I need this is because I'm working with self made dates. And in case I have a date '03.04.2018' I want to print it as 3. 4. 2018, without the 0s in the beginning. – Freddy19 Apr 30 '18 at 11:38
  • But how are you storing your date ? – AxelH Apr 30 '18 at 11:52

4 Answers4

7

Your question is pretty strange for one reason: An int value cannot start by 0. But if you store your value in a String, you could check it easily like this:

public static void main(String[] args) {
    String y = "0123";
    String n = "123";

    System.out.println(startByZero(y));
    System.out.println(startByZero(n));
}

public static boolean startByZero(String value) {
    return value.startsWith("0");
}

Output:

true
false

EDIT: Like Oleksandr suggest, you can use also:

public static boolean startByZero(String value) {
    return value.charAt(0) == '0';
}

This solution is more efficient than the first. (but at my own opinion it's also less readable)

Valentin Michalak
  • 2,089
  • 1
  • 14
  • 27
3

Your

y = 0123

will be considered as octal base but

 n = 123

is an decimal base.

Now when you do

if (y == n )

the numbers will be compared based on decimal base always.

You'll have to do conversions from octal to decimal or vice-versa based on your requirements.

You could also use Strings as @Valentin recommeneded.

Shanu Gupta
  • 3,699
  • 2
  • 19
  • 29
2

This doesn't seems on topic but based on OP's comment :

The reason I need this is because I'm working with self made dates. And in case I have a date '03.04.2018' I want to print it as 3. 4. 2018, without the 0s in the beginning.
Lommmp


If you don't want to reinvent the wheel, you should use java time API to parse and format your dates :

LocalDate date = LocalDate.parse("03.04.2018", DateTimeFormatter.ofPattern("MM.dd.yyyy"));
System.out.println(date.format(DateTimeFormatter.ofPattern("M.d.yyyy")));

3.4.2018

AxelH
  • 14,325
  • 2
  • 25
  • 55
-1

String.format("%01d",number);
for Zero padding with length 1.
like 0123
Read from: https://www.geeksforgeeks.org/java-string-format-examples/

LeoVS
  • 1
  • 2