4

Possible Duplicate:
Integer with leading zeroes

The program I am coding requires me to label an item with an inventory number of 012345 and store it in a int variable.

This is a stripped down example of what I am doing:

int test = 012345;
System.out.println(test);

this prints as:

5349

How do I get it to print out as 012345 rather than 5349?

EDIT: I am entering this into the parameter of a constructor for a custom class i am initializing. Then I use a method to return what the current number is, then print it to the terminal window.

Community
  • 1
  • 1
Matt Grixti
  • 43
  • 1
  • 6
  • 4
    `012345` literal makes Java sees your 10-supposed-base number as 8-base (octal) because of the 0 prefix. – Déjà vu Nov 13 '12 at 04:07

3 Answers3

9

You get a wrong number because when you prepend zero to an integer literal, Java interprets the number as an octal (i.e. base-8) constant. If you want to add a leading zero, use

int test = 12345;
System.out.println("0"+test);

You can also use the formated output functionality with the %06d specifier, like this:

System.out.format("%06d", num);

6 means "use six digits"; '0' means "pad with zeros if necessary".

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 2
    +1. *Very* unexpected behaviour. But it is not only Java that does this. – Thilo Nov 13 '12 at 04:07
  • @Thilo afaik, almost all language interpret int with leading zero to be octal number. – ksg91 Nov 13 '12 at 04:08
  • @Thilo This behavior is borrowed from C. Octal was a popular base in the PDP-11, because their machine code used groups of three bits to indicate register numbers and other parts of the instruction. – Sergey Kalinichenko Nov 13 '12 at 04:11
  • @dasblinkenlight This works, Thanks! Not sure if it is what is expected though. I added some additional information to my question to clarify. Mind letting me know if there is another work around you can think of? Thanks – Matt Grixti Nov 13 '12 at 04:27
  • @MattGrixti Take a look at an alternative that uses `format`. – Sergey Kalinichenko Nov 13 '12 at 04:31
0

As already said, int value with leading zero is considered as octal value. If you don't need to have test as int, why not make it string? Like

String test= new String("012345");

And if you want to use int for test, you can do not prepend 0, rather just use the number and prepend 0 at the time of printing.

In case if you're wondering how will you find how many leading zero are to be prepended, you may do like this

int lengthOfItemID=6;
int test=12345;
String test1=new String("000000"+test);
System.out.println(test1.substring(test1.length()-lengthOfItemID));

Pardon syntax mistakes, been years I last worked with java.

ksg91
  • 1,279
  • 14
  • 34
-1

You can get the right result by using Integer.parseInt. That will make your string into a decimal string. (found here). The JAVA API here states that it takes a string and returns a signed decimal.

Snakes and Coffee
  • 8,747
  • 4
  • 40
  • 60