-1

I'm working with a class called Human where I've defined a year to the Human class - the years are between 1932 and 2017 - and I would like to use String.format() to convert 1948 to F48, where the F is always in front. Is there an easy way to accomplish this? I've been trying out:

String sf3 = String.format("Value is %10s", 1948);

But it seems only to be adding more whitespace.

Grant Foster
  • 722
  • 2
  • 11
  • 21
A.Maine
  • 117
  • 9
  • 5
    Why have you been trying out `%10s` which is for padding (i.e. adds whitespace), and why are you surprised that it adds whitespace? Why would you think it might result in `F48`? – Kayaman Nov 06 '17 at 14:41
  • 3
    String sf3=String.format("Value is F%02d",1948%100); – iamnoten Nov 06 '17 at 14:44
  • 2
    By the way, you should hurry, your code will be deprecated in less than two month ;) – AxelH Nov 06 '17 at 14:47
  • It was more of a lead to use the String.format(), I'm not surprised that it adds whitespace, I simply dropped that line of code to show what I've been trying out in the past. – A.Maine Nov 06 '17 at 14:47
  • Thank you iamnoten! – A.Maine Nov 06 '17 at 14:51

1 Answers1

1

To drop the first two digits, you should use the modulo (%) operator to get the remainder when dividing by 100. Then just prefix that with the letter "F" and you're done.

Richard Schwartz
  • 14,463
  • 2
  • 23
  • 41