Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (10.2k points)

I am trying to use a .format method of a string. But if I place %1, %2, etc. in the string, java.util.UnknownFormatConversionException is thrown pointing to a confusing Java source code piece:

private void checkText(String s) {

    int idx;

    // If there are any '%' in the given string, we got a bad format

    // specifier.

    if ((idx = s.indexOf('%')) != -1) {

        char c = (idx > s.length() - 2 ? '%' : s.charAt(idx + 1));

        throw new UnknownFormatConversionException(String.valueOf(c));

    }

}

From this I understand that % char is forbidden. If so, then what should I use for argument placeholders?

I use Scala 2.8

1 Answer

0 votes
by (46k points)
edited by

Check out this Scala example:

val placeholder = "Hello %s, isn't %s cool?"

val formatted = placeholder.format("Ivan", "Scala")

I also hold a blog post regarding making format like Python's %operator that might be helpful.

Enroll in the Scala course in Toronto to get professionally certified.

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...