• 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

How to convert String into integer in JSTL

 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everybody,
I am having problem in converting JSTL variable into integer type in JSP (not using Spring). I am looking to do something like below:

Thanks In Advance!!
Cheers
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is totalAmount a String in the first place? That's what needs to be fixed. Or are you making it a String via the set action? Why are you using the set action at all?
 
Tarun Oohri
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks Bear Sir for replying .. 'totalAmount' is a String because it is being iterate from a list containing String objects , 'totalAmount' is one of the values from that list. Secondly i am using 'set' action so i could use it as a variable eg: <c:when test=""> tag. I would like to add that both the above code statements were not written in such order...2nd statement is written somewhere below , thats why i put 'totalAmount' value in a variable.

Any solution to this please?
 
Marshal
Posts: 28177
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
Okay, then why don't you convert it to an integer when you add it to the list?
 
Tarun Oohri
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Okay, then why don't you convert it to an integer when you add it to the list?


Thanks Paul , this could be possible solution. Actually that is is like List<String>, so have to remove this type safety and allow list to contain different type of objects .... Is that what you are trying to say ?
 
Paul Clapham
Marshal
Posts: 28177
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
Yes, that's the next thing I expected to have to say. Why do you have a list of different types of objects? In fact why do you have a list at all?
 
Tarun Oohri
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Yes, that's the next thing I expected to have to say. Why do you have a list of different types of objects? In fact why do you have a list at all?


Actually this list is being used to show set of data from the database, showing it in tabular way. That's why using list.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you change the type of list as integer if at all you know that the value stored is to be used as integer, alternatively you can write a scriptlet inside and manipulate the value although its not a good practice.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shail singh wrote:alternatively you can write a scriptlet inside and manipulate the value although its not a good practice.


In fact, it is such bad practice that it should never be recommended at all.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What it comes down to is that you are sending bad data to the JSP. The place to fix the data is before it gets to the JSP. Doing weird machinations in a JSP in order to deal with a poor data model is definitely not the way to go.
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
erm - Doesn't EL already "do the right thing" ?
It will automagically do the conversion from string to long if the other argument is a long.

So your code should be:



If total_amt is not actually a number this will throw a parse error at runtime.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Probably, (didn't test it) but I still think that scrubbed data should always be sent to a JSP.
 
Rancher
Posts: 4801
50
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Probably, (didn't test it) but I still think that scrubbed data should always be sent to a JSP.



This.
The data that goes to the JSP should be in a form that the JSP can use with a minimum of effort.
If you find yourself doing shenanigans on the data in the JSP then change the view model.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:

Bear Bibeault wrote:Probably, (didn't test it) but I still think that scrubbed data should always be sent to a JSP.


This.
The data that goes to the JSP should be in a form that the JSP can use with a minimum of effort.
If you find yourself doing shenanigans on the data in the JSP then change the view model.


I regret that I can only give this post one thumbs-up.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic