nealhudson Posted September 19, 2007 Share Posted September 19, 2007 HiI'd like to convert a string to a double. The issue is that sometimes the string does not contain numeric data. I was thinking of placing a try catch block in the value expression for a variable.Something like this: try{ new java.lang.Double(java.lang.Double.parseDouble($F{Time}));}catch(NumberFormatException e){ new java.lang.Double(0);} where $F{Time} is a string field returned from the database. However, I keep getting the following errors when trying to compile the report:Syntax error on token "(", ; expectedSyntax error on token ")", delete this token Link to comment Share on other sites More sharing options...
nealhudson Posted September 19, 2007 Author Share Posted September 19, 2007 This code works fine through a java compiler, stumped as to why it won't in iReports: try{ return new java.lang.Double(java.lang.Double.parseDouble($F{Time}));}catch(NumberFormatException e){ return new java.lang.Double(0);} Link to comment Share on other sites More sharing options...
svenn Posted September 19, 2007 Share Posted September 19, 2007 Straight java code is not supported in the expession box. If you want to write straight java code you can to do it in a Scriptlet or in your own class that you then point to in the report. Link to comment Share on other sites More sharing options...
nealhudson Posted September 19, 2007 Author Share Posted September 19, 2007 hmmm.. ok. So if straight java is not supported, is there any way in the expression box of doing what I want? (i.e. check to see if a string value contains a double, if it does then return it, if not then return 0). Link to comment Share on other sites More sharing options...
svenn Posted September 20, 2007 Share Posted September 20, 2007 Try the following expession in your expession variable box ($F{Time}.isNaN() ? do something : do something else ) Link to comment Share on other sites More sharing options...
csbac Posted September 20, 2007 Share Posted September 20, 2007 Hi!If you are using SQL ... any way of converting the string field into a double field in the SQL query already? Otherwise, the only way I see is writing a scriptlet that contains a "non-throwing" conversion function, and deploy the scriptlet together with the report, as svenn already mentioned ... Sebastian Link to comment Share on other sites More sharing options...
nealhudson Posted September 20, 2007 Author Share Posted September 20, 2007 [deleted] Post edited by: nealhudson, at: 2007/09/20 15:38 Link to comment Share on other sites More sharing options...
nealhudson Posted September 20, 2007 Author Share Posted September 20, 2007 [deleted] Post edited by: nealhudson, at: 2007/09/20 15:38 Link to comment Share on other sites More sharing options...
margaret Posted September 28, 2009 Share Posted September 28, 2009 In the text field expression try enteringnew Double($F{Time})and then changed the expression class tojava.lang.double Link to comment Share on other sites More sharing options...
neruv Posted March 7, 2011 Share Posted March 7, 2011 Works with...Set " Text Field Expression " : Double.parseDouble($F{PARAM})Set " Expression Class " : java.lang.DoubleAdd rt.jar (from java runtime) to classpath [ Tools >> Options >> Classpath]And Compile Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now