Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to create a for loop in scriplet code  RSS feed

 
Will Johnson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am trying to create a for loop in Java:



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:


 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66142
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Rather than looping in scriptlets which have been discredited for almost a decade, have you considered using the JSTL's <c:forEach> tag?
 
Will Johnson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi No I haven't considered using the <c:forEach> tag. How would I use it? and would I be able to print the output after a specific index and have the loop continue on?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66142
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't know about the JSTL and EL then your JSP knowledge is very out of date. I'd recommend grabbing a good modern book or tutorial on JSP that focuses on JSTL. Or read through the JSTL Specification (link in JspFaq).
 
Alex Hurtt
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While Bear gives good advice regarding updating your jsp knowledge, it appears that there are deeper issues at play here with this code. First, I want to make sure my interpretation of what you are actually aiming to do is correct. The reason the Southeast Teams header appears once for every loop iteration is, oddly enough, because you have it inside the body of your loop. So if your list contains 8 total elements you're going to see it 8 times the way you have it now. Plus, have you examined the HTML source code output from this script? Is it legal HTML? Looks like you are going to be outputting a table tag once then followed by an h2 tag for each iteration of your loop, but this h2 tag doesn't fall within any kind of td or th element like it should if it were part of a table...assuming this is what you wanted to do anyway which it probably isn't. Did you intend for the output to be in a single 2 column table with 1 column for Northeast Teams and 1 column for southeast teams? Or did you intend for it to be two separate tables one after the other? I think this isn't really so much of a jsp specific problem but possibly just a basic programming logic problem.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!