3

I'm new at this and I'm currently editing a friend's existing code, I just wanted to know if there's a simple way of doing a nested for loop in JSTL/JSF for a Java code similar to this one:

blocks=0;
for(vElement=0; blocks<19; vElement++){
   for(hElement=0; exit<1; hElement++){
      System.out.println(blocks);
      if(blocks!=18){
          blocks++;
      } else{
          exit = 1;
      }
   }
   System.out.println("\n");
}

The output would be something like this:

0   1   2   3   4   5   6
7   8   9   10  11  12  13
14  15  16  17  18

Everything I've seen on here has something to do with a Backing Bean (and I don't really need that for this one). Any suggestions?

Nicole
  • 39
  • 1
  • 9
  • 3
    Am I missing something, or is this a very convoluted way to print the numbers 0 - 18? – slipset May 22 '12 at 08:00
  • look at BalusC answer... http://stackoverflow.com/a/8037296/617373 – Daniel May 22 '12 at 08:10
  • @slipset I used numbers to express my problem with the loops, the real code doesn't print that. I wanted to see how the loops would look like if it's just a simple problem like this one. :) – Nicole May 22 '12 at 09:00

1 Answers1

3
<c:forEach var="i" begin="0" end="2">
  <c:forEach var="j" begin="0" end="6">
     <c:if test="${(i*7 + j) <=18}">
                       <c:out value="${(i*7 + j)}" />
     </c:if>
  </c:forEach>
  <br />
</c:forEach>

Note: haven't tested

Cloning BalusC's request I have also added new request

Also See

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438