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

Adding gitlab solution reference and indent() explanation

parent 29d67a7f
......@@ -4041,50 +4041,34 @@ System.out.format("Start:<emphasis role="red">%5d</emphasis>:End", <emphasis
</question>
<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;
&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;
&lt;/body&gt;
&lt;/html&gt;""");
}</programlisting>
<para>A complete solution is being provided by <link
xlink:href="https://gitlab.mi.hdm-stuttgart.de/goik/GoikLectures/-/blob/master/Doc/Sd1/Ref/Statements/MultSimpleHtmlTable/src/main/java/de/hdm_stuttgart/mi/sd1/App.java">App.java</link>.
This solution uses both <xref linkend="glo_Java"/> 15 multi
line blocks and the String.<link
xlink:href="https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/String.html#indent(int)">indent(..)</link>
method. Consider the following example:</para>
<programlisting language="java">final String block = """
A simple block
consisting of
three lines""";
System.out.println(block);
System.out.println("\nAgain using indentation of 10 spaces:");
System.out.println(block.indent(10));
</programlisting>
<para>This creates:</para>
<screen>A simple block
consisting of
three lines
Again using indentation of 10 spaces:
A simple block
consisting of
three lines</screen>
</answer>
</qandaentry>
</qandadiv>
......
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