-3

I am working on a java program and I need to store some integers starting from 0 to 16 as 00, 01 and so on...

Now when I store '09' for 9 or '08' for 8 it starts giving me error.

From this and this question I know that it is octal notation.

My question is :

IS there a way to store 08 or 09 in integer in JAVA?

I HAVE to store numbers as 00, 01 .. only what is alternative?

Thanks a lot!

Community
  • 1
  • 1
Nullpointer
  • 1,086
  • 7
  • 20
  • 4
    Why do you need it in this format? For output or something else? – AntonH Jan 28 '14 at 16:08
  • 1
    What situation do you want to use 00, 01 etc. in? – Someone Jan 28 '14 at 16:08
  • 4
    It is unlikely you need to *store* them like that, you probably need to *display* them like that - is that fair? – Fenton Jan 28 '14 at 16:08
  • You'ld have to use `System.out.printf()` to format output, but you can't use it internally as "08", "09", etc. – AntonH Jan 28 '14 at 16:09
  • No! I want to use these integers later on That is why I am storing them as integers, otherwise I would have used String – Nullpointer Jan 28 '14 at 16:09
  • 2
    @Nullpointer: In what way do you think that 08 is different to 8, in terms of integer values? – Jon Skeet Jan 28 '14 at 16:10
  • leading 0 is not part of the number, it is part of the formating. – njzk2 Jan 28 '14 at 16:11
  • When I print the values I need 09 but I also need to do some integral manipulation on those Hence I can not store them as Strings(I don't want to use Integer.ParseInt(String) everytime) – Nullpointer Jan 28 '14 at 16:12
  • 3
    Write a print or toString method to handle printing your ints the way you want... – AnxGotta Jan 28 '14 at 16:16

5 Answers5

6

Store your numbers with an Integer, and if you need to display these values cast them in a String. The String could be easily format to show the leading zero.

    int num = 5;
    System.out.println(String.format("%02d", num));
VWeber
  • 1,261
  • 2
  • 12
  • 34
2

No, you can't do it unless you store them as string. Or store them as regular int and use string format for the output.

evanwong
  • 5,054
  • 3
  • 31
  • 44
2

It just makes no sense to try to store 09 as a numeric value (unless you really mean 9 in octal, which would make even less sense). The 09 value you're referring to is a representation of the value 9. Numeric values don't have anything to do with how they are represented. You could represent the value nine in decimal base as 9 or 09 or 9.0, or in octal base as 011, but the value remains the same, nine, and it should be stored in an Integer variable (or an int).

Store the numbers in Integer variables as the numeric values they are, and output them with the format you wish to with String.format() for instance:

 Integer i = 9; // or Integer i = 011; whatever
 System.out.println(String.format("%02d", i));
Xavi López
  • 27,550
  • 11
  • 97
  • 161
2

Try with String.format()

int num = 9;
System.out.println(String.format("%02d", num));

In %02d

  • 0 - to pad with zeros
  • 2 - to set width to 2

Output

09
Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
0

It sounds like you are trying to do something wrong; there are a number of possible ways however.

Case 1: You are trying to store a date component and display it as two digits. In this case, use java.util.Calendar instead.

Case 2: You are trying to store part of a version number. In this case, you should be storing it as a String, not an int.

Case 3: You are trying to store some other type of data, and need to display it with a leading zero. For this, you should be using PrintStream.printf or String.format bto do this. System.out.printf("%1$2d", myInteger) will output the number as two digits, adding a leading zero if needed.

AJMansfield
  • 4,039
  • 3
  • 29
  • 50