Commit bf397d35 authored by Dr. Martin Goik's avatar Dr. Martin Goik

HTML table using multiline statements

parent a1a7509b
......@@ -3997,11 +3997,25 @@ System.out.format("Start:<emphasis role="red">%5d</emphasis>:End", <emphasis
<qandaentry>
<question>
<para>Modify the previous code to generate HTML output instead
of pure text and watch the result in a web browser:</para>
of pure text and watch the result using a web browser. The
expected output reads:</para>
<programlisting language="xml">&lt;html xmlns='http://www.w3.org/1999/xhtml'&gt;
&lt;head&gt;
&lt;title&gt;A square table&lt;/title&gt;
&lt;title&gt;A square table&lt;/title&gt;
&lt;!-- CSS based styling --&gt;
&lt;style&gt;
table,td,th {
border: 1px solid black;
}
table {
border-collapse: collapse;
}
td {
text-align: right;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;table&gt;
......@@ -4009,13 +4023,16 @@ System.out.format("Start:<emphasis role="red">%5d</emphasis>:End", <emphasis
&lt;th&gt;n&lt;/th&gt;&lt;th&gt;n * n&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style='text-align: right;'&gt;0&lt;/td&gt;&lt;td style='text-align: right;'&gt;0&lt;/td&gt;
&lt;td&gt;<emphasis role="red">0</emphasis>&lt;/td&gt;&lt;td&gt;<emphasis
role="red">0</emphasis>&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style='text-align: right;'&gt;1&lt;/td&gt;&lt;td style='text-align: right;'&gt;1&lt;/td&gt;
&lt;td&gt;<emphasis role="red">1</emphasis>&lt;/td&gt;&lt;td&gt;<emphasis
role="red">1</emphasis>&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style='text-align: right;'&gt;2&lt;/td&gt;&lt;td style='text-align: right;'&gt;4&lt;/td&gt;
&lt;td&gt;<emphasis role="red">2</emphasis>&lt;/td&gt;&lt;td&gt;<emphasis
role="red">4</emphasis>&lt;/td&gt;
&lt;/tr&gt;
...
&lt;/table&gt;
......@@ -4025,34 +4042,48 @@ System.out.format("Start:<emphasis role="red">%5d</emphasis>:End", <emphasis
<answer>
<programlisting language="java">public static void main(String[] args) {
final int LIMIT = 20; // The number of records to be printed
System.out.print(
"" + "&lt;html xmlns='http://www.w3.org/1999/xhtml'&gt;\n"
+ " &lt;head&gt;\n"
+ " &lt;title&gt;A square table&lt;/title&gt;\n"
+ " &lt;/head&gt;\n"
+ " &lt;body&gt;\n"
+ " &lt;table&gt;\n");
System.out.println(" &lt;tr&gt;");
System.out.println(" &lt;th&gt;n&lt;/th&gt;&lt;th&gt;n * n&lt;/th&gt;");
System.out.println(" &lt;/tr&gt;");
for (int i = 0; i &lt;= LIMIT; i++) { // Printing the table's body
System.out.println(" &lt;tr&gt;");
System.out.println(" &lt;td style='text-align: right;'&gt;" + i +
"&lt;/td&gt;&lt;td style='text-align: right;'&gt;" + i * i + "&lt;/td&gt;");
System.out.println(" &lt;/tr&gt;");
final int LIMIT = 20; // The number of records to be printed
System.out.print("""
&lt;html xmlns='http://www.w3.org/1999/xhtml'&gt;
&lt;head&gt;
&lt;title&gt;A square table&lt;/title&gt;
&lt;!-- CSS based styling --&gt;
&lt;style&gt;
table,td,th {
border: 1px solid black;
}
table {
border-collapse: collapse;
}
td {
text-align: right;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;th&gt;n&lt;/th&gt;&lt;th&gt;n * n&lt;/th&gt;
&lt;/tr&gt;
""");
final int trBlockIndent = 6; // Each &lt;tr&gt;...&lt;/tr&gt; is being indented by 6 spaces
for (int i = 0; i &lt;= LIMIT; i++) { // Printing the table's body
System.out.print(("""
&lt;tr&gt;
&lt;td&gt;""" + i + "&lt;/td&gt;").indent(trBlockIndent));
System.out.print(("""
&lt;td&gt;""" + i * i + "&lt;/td&gt;").indent(trBlockIndent));
System.out.print("""
&lt;/tr&gt;
""".indent(trBlockIndent));
}
System.out.print(
"" + " &lt;/table&gt;\n"
+ " &lt;/body&gt;\n"
+ "&lt;/html&gt;\n");
System.out.print("""
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;""");
}</programlisting>
</answer>
</qandaentry>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment