user1340431
user1340431

Reputation: 29

Creating a 3 column html table from arraylist?

How can I create a 3 column table in HTML from an ArrayList?

My current code looks like this:

<table border="0">

        <%
            for (int i = 1; i < states.size(); i++) {
        %>
        <TR>
            <%
                for (int col = 1; col <= 3; col++) {
            %>
            <TD>
                <%
                    out.println(states.get(i));}
                %>
            </TD>
            
        </TR>
        <%
            }
        %>
    </table>

I get a 3 column format table but 3x the same entry in each row...

Expected output

Albania | Algeria   | American Samoa
Andorra | Angola    | Anguilla
..

What am I missing?

Upvotes: 1

Views: 5646

Answers (4)

Aung Thaw Aye
Aung Thaw Aye

Reputation: 437

Please try like this.. Just add another variable which will be used when retrieving info from your array list.

<table border="0">

    <%

        for (int i = 1, index = 0; i < states.size(); i++) {
    %>
    <TR>
        <%
            for (int col = 1; col <= 3; col++) {
        %>
        <TD>
            <%
                out.println(states.get(index++));}
            %>
        </TD>

    </TR>
    <%
        }
    %>
</table>

Upvotes: 0

Andrew Thompson
Andrew Thompson

Reputation: 168825

If I understand the requirement correctly (not sure that is the case), then something like this will give you a start.

<table border="0">
    <tr>
    <%
        for (int i = 1; i < states.size(); i++) {
            out.println("<td>" + states.get(i) + "</td>");
            if (i>0 && i%3==0) {
                out.println("</tr><tr>");
            }
        }
    %>
    </tr>
</table>

It will produce output something along these lines..

Albania | Algeria   | American Samoa
Andorra | Angola    | Anguilla
..

Note that it still has problems. If there are 'multiples of 3' countries, there will be an entirely empty line at the end of the table. If not, the last row will not have the correct number of columns. BNI.

Upvotes: 1

Rudy
Rudy

Reputation: 7044

Note :

  • Index in List start with 0
  • You need to compute the index before you retrieve.

        <%
            for (int i = 0; i < states.size(); i++) {
        %>
        <TR>
            <%
                for (int col = 0; col < 3; col++) {
            %>
            <TD>
                <%  
                    int index= (i*3)+col;
                    if(states.size()>index+1)
                    {    
                     out.println(states.get(index));
                    }
                    i = i+1;  
                %>
            </TD>
    
        </TR>
        <%
            }
        %>
    </table>
    

Upvotes: 0

Michael
Michael

Reputation: 2261

It's just a small typo. You're indexing with 'i', but your internal variable is col.

Change it to:

...
out.println(states.get(3*col + i));
...

Try that one, you might need to zero index your column now.

Upvotes: 0

Related Questions