1

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?

12
  • What are you trying to put in the other columns? Commented Apr 18, 2012 at 5:55
  • All the columns & rows should be filled with different state/countries. Commented Apr 18, 2012 at 5:57
  • So is there another variable that stores countries? It looks like there is an array of states, but I do not see an array of countries. Did you want to format each cell with something like California/USA? Commented Apr 18, 2012 at 6:01
  • Sorry, I should be more precise... I have just 1 array with states which I want to display in a 3column table No countries ;) Commented Apr 18, 2012 at 6:03
  • 1
    Sorry, i'm a bit confused, would you be able to provide an example of what you want? For instance, what is in the first row, second row, etc. Commented Apr 18, 2012 at 6:07

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

You're welcome. :) BTW - are you certain that the very first country is listed? I suspect int i = 1 should be int i = 0
haha :D i recognized it! But now I have a 4th column just for one country Oo
0

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.

2 Comments

now the table looks like this: Col1 Col2 Col3 Alaska Arizona Arkansas Alaska Arizona Arkansas Alaska Arizona Arkansas Alaska Arizona Arkansas Alaska Arizona Arkansas :/
Ohh, I get it now. You have one array and you want to index it like a 3x(size/3) table. Ok...
0

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>
    

1 Comment

made a silly mistake. I have edited the answer. Please try again.
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.