0

I am trying to add number '0' to numbers less than 10

For example instead of '9', output is supposed to be '09'

I have tried this but it still outputs '0' for number > 10 :

System.out.println("0" + message.substring(i, Math.min(i + framel, message.length())).length() +
                "-" + message.substring(i, Math.min(i + framel, message.length())));

I also tried this (length < 10 ? "0" : ""):

 System.out.println((length < 10 ? "0" : "") + message.substring(i, Math.min(i + framel, message.length())).length() +
                "-" + message.substring(i, Math.min(i + framel, message.length())));
newborn
  • 159
  • 1
  • 2
  • 12
  • The first one prints "0" as you always print "0". The second one prints "0" most likely because you check an unrelated variable i.e. `length < 10` is always true. – Peter Lawrey Nov 23 '15 at 20:37
  • Yea I forgot to mention the length variable comes from some other code i.e. int length = message.length(); – newborn Nov 23 '15 at 20:41
  • I assume `message.length()` is the length of the message not the length of the number you are printing. In which case I would have though it never prints the "0". I assume `i` is not always `0` so the number you are printing will be shorter than the length of the message. – Peter Lawrey Nov 23 '15 at 20:47

0 Answers0