-2

I have a text like:

DISK_ERROR_MESSAGE = "Server %s is using more than 90% of the disk."

The problem, is when I try to do something like:

String.format(DISK_ERROR_MESSAGE,host.getName())

Java launches the error:

Method threw 'java.util.MissingFormatArgumentException' exception.
java.util.MissingFormatArgumentException: Format specifier '% o'

The problem is that % of the 90%.

But I don't know how to avoid it without separating the text in two strings.

Shudy
  • 7,416
  • 17
  • 56
  • 94

1 Answers1

-1

To escape %, you will need to double it up: %%.

So, you need to change your string to

DISK_ERROR_MESSAGE = "Server %s is using more than 90%% of the disk."
vinS
  • 1,397
  • 5
  • 23
  • 36
  • 1
    This is a literal copy-paste of answer https://stackoverflow.com/a/5011981/5771446 – Sync Dec 18 '17 at 16:05
  • Yes. I know. Just checking downvotes. Yesterday I had a discussion over it with a senior member. They are saying that we should answer duplicate questions. – vinS Dec 18 '17 at 16:07
  • What? Is there a meta discussion on this? – Sync Dec 18 '17 at 16:08
  • Yes :) There are so called _seniors_ who got reputation bu asking duplicate questions and duplicate answer. – vinS Dec 18 '17 at 16:10
  • I cannot delete this answer, as it is accepted :( – vinS Dec 21 '17 at 03:47