• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

break in the case of switch statement inside for loop

 
Greenhorn
Posts: 9
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Any help for this code would be greatly appreciated!

I have a code in the jsp shown below:

for(int i= 0; i< length; i++) {
switch(i){

case 0:
for(int j = 0; j < length; j++) {
<td>
//Do something
</td>
}
break;
case 1:
for(int j = 0; j < length; j++) {
<td>
//Do something
</td>
}
break;
case 2:
for(int j = 0; j < length; j++) {
<td>
//Do something
</td>
}
break;


Please note the places where I have used breaks. The JSP prints a message unreachable code as soon as I add break. Otherwise it loops through the switch and for loop and creates 'n' number of td's. Any limitation of using the break in this scenario?Please help
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
at what line exactly you are getting the unreachabe code.I think you are getting this problem in last break.just remove that and try.
 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Avoid using scriplets in jsp, it's a very old practice. Look for JSTL, EL, Custom tags etc.
 
Vanitha Pai
Greenhorn
Posts: 9
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Eshwin Sukhdeve wrote:at what line exactly you are getting the unreachabe code.I think you are getting this problem in last break.just remove that and try.



Hi Eshwin Sukhdeve,

Thanks for the suggestion but I have tried that and even then it doesn't work:(
 
Vanitha Pai
Greenhorn
Posts: 9
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Swastik Dey wrote:Avoid using scriplets in jsp, it's a very old practice. Look for JSTL, EL, Custom tags etc.



Thanks for the suggestion but this code is in a maintainence(very old)application and we have to use the scriptlets. It doesn't support JSTL and custom tags.
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If thats the case then please post the code with "code" tags so that it gets formatted correctly.

You seem to be missing the scriptlet markers (<% / %>)delimiting what is java code and what is template code.
and also you are missing two '}' characters at the end to close your switch and outer loop.

yes. There is a reason we have moved on from mixing scriptlets and template text.
because this stuff is so hard to read and debug!

 
Vanitha Pai
Greenhorn
Posts: 9
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stefan Evans wrote:If thats the case then please post the code with "code" tags so that it gets formatted correctly.

You seem to be missing the scriptlet markers (<% / %>)delimiting what is java code and what is template code.
and also you are missing two '}' characters at the end to close your switch and outer loop.

yes. There is a reason we have moved on from mixing scriptlets and template text.
because this stuff is so hard to read and debug!





Thanks for pointing out that the two curly braces were missing. Now I have added the code within the code tags and also the scriplets for java code and temlate code outside the scriptlets.

Please let me know if I have missed something.


 
Stefan Evans
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So if I copy and paste this code into a jsp and try running it it will work right?
No?

# You don't declare 'length' anywhere
# You're missing a <% on line 23

However once those issues are fixed, it works exactly as I would expect it to.
I still think it would be better with JSTL. The technology has been around more than 10 years. What sort of outdated system are you using that its not available?

If you must persist with scriptlet code then ensure:
- all your brackets are lined up
- all your open/close scriptlet tags are in the right place

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see why you would write

when you could use equivalent code which is much simpler:

 
Vanitha Pai
Greenhorn
Posts: 9
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:I don't see why you would write

when you could use equivalent code which is much simpler:



Hi Paul,

Could you please elaborate the solution? I am not sure I understand the solution what you have mentioned. Thanks for your time.
 
There were millions of the little blood suckers. But thanks to this tiny ad, I wasn't bitten once.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic