Question

<body>
    <%
        int apps = 8;
        out.println("<div>");
        out.println("<table>");
        StringBuilder Row1 = new StringBuilder();
        Row1.append("<tr>");
        StringBuilder Row2 = new StringBuilder();
        Row2.append("<tr>");
        for (int i = 0; i < apps; i++) {
            if (i % 2 == 0) {
                Row1.append("<td>" + i + "</td>");
            }
            if (i % 2 == 1) {
                Row2.append("<td>" + i + "</td>");
            }
        }
        Row1.append("</tr>");
        Row2.append("</tr>");
        out.println(Row1.toString());
        out.println(Row2.toString());
        out.println("</table>");
        out.println("</div>");
    %>
</body>

This is my jsp page. Currently I'm getting output as in Row1: 0 2 4 6 and in Row2: 1 3 5 7. But I need output as in Row1: 1 2 3 4 and in Row2: 5 6 7 8. How can I do that?

Was it helpful?

Solution

I changed only your conditions in if clause

for (int i = 0; i < apps; i++) {
            if ( i<4) {
                Row1.append("<td>" + i + "</td>");
            }
            if (i>3 && i<8) {
                Row2.append("<td>" + i + "</td>");
            }
        }

Output :

 Row1 : 0123  
 Row2 : 4567

Working demo : http://ideone.com/PMiknB .dint tried any html part

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top