Create an HTML table: Difference between revisions

(Added XPL0 example.)
Line 2,693:
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.util.Random;
</syntaxhighlight>
<syntaxhighlight lang="java">
String generateHTMLTable() {
StringBuilder string = new StringBuilder();
string.append("<table border=\"1\">");
string.append(System.lineSeparator());
string.append("<tr>".indent(2));
string.append("<th width=\"40\"></th>".indent(4));
string.append("<th width=\"50\">X</th>".indent(4));
string.append("<th width=\"50\">Y</th>".indent(4));
string.append("<th width=\"50\">Z</th>".indent(4));
string.append("</tr>".indent(2));
Random random = new Random();
int number;
for (int countA = 0; countA < 10; countA++) {
string.append("<tr>".indent(2));
string.append("<td>%d</td>".formatted(countA).indent(4));
for (int countB = 0; countB < 3; countB++) {
number = random.nextInt(1, 9999);
string.append("<td>%,d</td>".formatted(number).indent(4));
}
string.append("</tr>".indent(2));
}
string.append("</table>");
return string.toString();
}
</syntaxhighlight>
<table border="1">
<tr>
<th width="40"></th>
<th width="50">X</th>
<th width="50">Y</th>
<th width="50">Z</th>
</tr>
<tr>
<td>0</td>
<td>7,382</td>
<td>8,078</td>
<td>1,200</td>
</tr>
<tr>
<td>1</td>
<td>121</td>
<td>3,051</td>
<td>1,795</td>
</tr>
<tr>
<td>2</td>
<td>9,310</td>
<td>6,264</td>
<td>7,526</td>
</tr>
<tr>
<td>3</td>
<td>2,294</td>
<td>367</td>
<td>3,753</td>
</tr>
<tr>
<td>4</td>
<td>9,680</td>
<td>3,921</td>
<td>6,582</td>
</tr>
<tr>
<td>5</td>
<td>5,308</td>
<td>5,232</td>
<td>3,035</td>
</tr>
<tr>
<td>6</td>
<td>1,252</td>
<td>1,468</td>
<td>7,081</td>
</tr>
<tr>
<td>7</td>
<td>1,808</td>
<td>1,143</td>
<td>4,439</td>
</tr>
<tr>
<td>8</td>
<td>7,133</td>
<td>1,747</td>
<td>8,636</td>
</tr>
<tr>
<td>9</td>
<td>4,882</td>
<td>9,367</td>
<td>5,056</td>
</tr>
</table>
<br />
An alternate demonstration
{{works with|Java|1.5+}}
This example assumes the header row is the first row in the given array and does not add row numbers. They will need to be added by the programmer when constructing the array.
118

edits