Java – Formatting Output

Format Output:

  • It is important to format the results before display to the user.
  • Write() method display information on web page.
  • We display information by concatenating with proper messages to make the user more readable.

The table giving the examples to concatenate the messages with values:

SyntaxExample
int + int -> int10 + 20 = 30
String + String -> String“Hello” + “World” -> HelloWorld
“10” + “20” -> 1020
String + int -> String“Sum = “ + 10 -> Sum = 10
String + int + int -> String“Sum = “ + 10 + 20 -> Sum = 1020
String + (int + int) -> String“Sum = “ + (10+20) -> Sum = 30
String + double -> String“PI = “ + 3.142 -> PI = 3.142
String – int -> Error“Sum = “ – 10 -> Error

Code:

<!doctype html>
<html>
            <body>
                        <script>
                                    document.write(10 + 20 + “<br/>”);
                                    document.write(“Hello” + “World” + “<br/>”);
                                    document.write(“Sum = ” + 10 + “<br/>”);
                                    document.write(“PI value = ” + 3.142 + “<br/>”);
                                    document.write(“Sum = ” + 10 + 20 + “<br/>”);
                                    document.write(“Sum = ” + (10 + 20) + “<br/>”);
                        </script>
            </body>
</html>

Output:

Scroll to Top