0

I am writing some code and i need to elaborate a class to build zip codes. My zip code in portugal ends with XXXX-032 and i inserted 032 into the 'int' parameter and java reads 032 as 26. Why does this happen?

For now instead of 032 i am using only 32 to be easier.

public CodigoPostal(int fourDigits, int threeDigits) {
    this.parcela1 = fourDigits;
    this.parcela2 = threeDigits; 
}

CodigoPostal code1 = new CodigoPostal(3421, 32);
System.out.println(code1);
System.out.println(code1.getThreeDigits());

I expected the first sout to be 3421-032 and the second 032 but the acutal is 3421-026 and 26. Why does java understand 032 as 26?

deHaar
  • 17,687
  • 10
  • 38
  • 51

1 Answers1

-1

Maybe 032 is octal format number because starts with 0...

  • I will try to work a way to reverse this problem but i am not seeing a way to convert octal to integer in case the user inputs 032... – Rafael Moreira May 14 '19 at 09:23
  • trim the leading 0 's using regexp.s = s.replaceFirst ("^0*", ""); – akshaya pandey May 14 '19 at 09:26
  • @akshayapandey or just don't put that zero in your code. Parsing an int will ignore leading zeroes. It only switches to octal if you put one on your integer literals inside Java code. Fundamentally, the problem is trying to represent an int as a string, just because this string is mostly made of digits so it looks like it represent a number/quantity as int are made for. – kumesana May 14 '19 at 09:31