Search code examples
javaprintform

JAVA: Remainder % with printf() not working


I am trying to write the equivalent of

System.out.println(a + " % " + b + " = " + r);  

with the print(f)

System.out.printf("%d +'%' + %d = %d",a,b,r);

but it says the format string is malformed. I am guessing it is because of the % sign. Is there a way around this?


Solution

  • System.out.printf("%d %% %d = %d%n", a, b, r);
    

    Self-escaped.

    Your code constructed a string with a single %. Also add a newline, by %n which can deliver \n or \r\n.