0

Hi I am trying to create a for loop in Java:

<%
            for (int i = 0; i < webList.size(); i++) {

                            WebBean WebBean = (WebBean) webList.get(i);

                            System.out.println (i);
                            //for (int x = 0; x < webList.size(); x++) {
                            out.println( "<h2>SouthEast Teams</h2> " );
                            //}


    %>

What I would like to do is split up the array at index 5 so that It creates two different list. I am currently using XML to create the list and it works okay except when I added in the heading for the second list "southeast teams" is appearing 8 times above the "northeast teams" heading and not after the team at index 5.

Below is my full JSP code:

<%
    ArrayList webList = (ArrayList) request
                    .getAttribute(ConstantKeys.WEB_LIST);

%>

<h2 tabindex="0" id="contentBody">NorthEast Teams</h2>
<%
    if (webList != null) {       
%>
<table>

    <%
            for (int i = 0; i < webList.size(); i++) {

                            WebBean WebBean = (WebBean) webList.get(i);

                            System.out.println (i);
                            //for (int x = 0; x < webList.size(); x++) {
                            out.println( "<h2>SouthEast Teams</h2> " );
                            //}


    %>
    <tr>
            <td class="col1">
            <div class="buttonWrap"


                    title="<%=WebBean.getTeamName()%>" class="button"><%=WebBean.getTeamName()%></a></div>
            </td>
            <td tabindex="0"><%=WebBean.getLocation()%></td>
    </tr>
    <%
            }
    %>
</table>

<%
    } 
%>
4
  • just put an if statement in the for loop and when it hits 5 add the <h2>southeast</h2> Commented Jun 3, 2011 at 1:27
  • how do I add an if statement inside of a for loop? Commented Jun 3, 2011 at 1:29
  • so it would be for (int i = 0; i < webList.size(); i++) { WebBean WebBean = (WebBean) webList.get(i); if (i == 5){ out.println( "<h2>SouthEast Teams</h2> " ); } Commented Jun 3, 2011 at 1:33
  • @will was that what you were looking for Commented Jun 3, 2011 at 1:47

1 Answer 1

1

Add A if statement that will check the how far you have iterated through the loop. (in this case you want to check 5)
edit: Add a variable that will be the "numberOfTeams" so you can change the value there and not in the if statement in case of future changes.

     <%
                for (int i = 0; i < webList.size(); i++) {

                                WebBean WebBean = (WebBean) webList.get(i);
                                if(i == 5){
                                    System.out.println (i);
                                    //for (int x = 0; x < webList.size(); x++) {
                                    out.println( "<h2>SouthEast Teams</h2> " );
                                    //}
                                }

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

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.