-3

The issue is that I do not understand how to make two 0's appear before the 7, after the print. the print keeps coming up with just the 7 after the + bond is used in the print line.

int bond = 007; System.out.println("insert what ever you want " + bond); insert what ever you want 7

What i want it so say is

insert what ever you want 007

  • See http://stackoverflow.com/questions/21720130/is-there-any-way-to-generate-id-combined-with-alphabets-in-java/21720170#21720170 – user2864740 Feb 20 '14 at 21:54
  • Please make sure to phrase a proper question. Also, please format your code for best readability. This will greatly improve your chances of getting a good answer! – Martijn de Milliano Feb 20 '14 at 22:12

3 Answers3

2
System.out.println(String.format("%02d", Bond));
Saddam Abu Ghaida
  • 6,381
  • 2
  • 22
  • 29
0

Because it is an int, it will trim the 0 from the start of the number. This is because 007 is numerically equal to 7. Use a String instead:

String bond = "007";

System.out.println("Insert whatever you want " + bond);

Alternatively, if you have to use an int, then you can use the format method as so:

System.out.println("Insert whatever you want" + String.format("%02d", bond));

Edit

Excellent point made in the comments. 007 will be interpreted as an octal number, which means 007 will actually be interpreted as 042. Agent 042 is the jealous spy, who always tries to pull off what 007 can, but never quite succeeds. We don't want him taking any of 007's glory.

christopher
  • 26,815
  • 5
  • 55
  • 89
  • (In addition, Java actually treats a numeric integer literal starting with a zero as octal. Agent 042 - `042 != 42` - will have a nasty surprise when he tries to cash a paycheck!) – user2864740 Feb 20 '14 at 21:55
0

You could use System.out.printf() like so,

public static void main(String[] args) {
    System.out.printf("%03d - %s, %s.\n", 7, "Bond", "James Bond");
}

Outputs,

007 - Bond, James Bond.
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249