aspose file tools*
The moose likes JSP and the fly likes break in the case of switch statement inside for loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "break in the case of switch statement inside for loop " Watch "break in the case of switch statement inside for loop " New topic
Author

break in the case of switch statement inside for loop

Vanitha Pai
Greenhorn

Joined: Mar 27, 2012
Posts: 9

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
Eshwin Sukhdeve
Ranch Hand

Joined: Mar 15, 2012
Posts: 80
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.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1478
    
    6

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


Swastik
Vanitha Pai
Greenhorn

Joined: Mar 27, 2012
Posts: 9

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

Joined: Mar 27, 2012
Posts: 9

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.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
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

Joined: Mar 27, 2012
Posts: 9

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

Joined: Jul 06, 2005
Posts: 1018
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

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18882
    
    8

I don't see why you would write

when you could use equivalent code which is much simpler:

Vanitha Pai
Greenhorn

Joined: Mar 27, 2012
Posts: 9

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: break in the case of switch statement inside for loop